Objective

  • Input/Output from and to Arduino.
  • Make the LED turn on when we input certain command.
  • Make the LED turn off when we input certain command.

Prerequisite

Requirement

Following is all materials I use for this tutorial.

  • Arduino Uno R3
  • LED

You can change the Arduino board to higher specification if you like. The LED I use will emit red light as it is powered by electricity.

You also need an USB A to B cable for upload our program to our Arduino and also for serial communication.

Circuit

Here is the circuit image, created using Fritzing.

Blinking LED

To build the circuit, attach the LED as shown in the figure. Attach the long leg of an LED (positif leg or anode) to pin 13. Attach the short leg (negative leg or cathode) to the ground (GND). On Arduino Uno, there is also a built in status LED which connected to pin 13. When our Arduino running successfully, both status LED and external LED will start to blink.

We don’t need extra resistor for this project, but make sure you are using pin 13. If you do connect it to other pin, it might be destroyed. The reason is that pin 13 has an internal resistor that other pins don’t have which limit current to our LED.

ExampleCircuit_sch

Code

Let’s see what code we use for this project and discuss it later.

Notice that we define two constant here, LED_PIN which is pin we use, and BAUD_RATE for the baud rate. Baud rate is the rate of character transferred every second. Notice that 1 character is 1 byte, which is also 8 bit. Both party who communicate must agree on same baud rate before transmission began. If baud rate is not equal, data can’t be transferred correctly.

Here we introduce a object called Serial. This object handles input output in serial communication. To use this object we should initialize which baud rate we use. In this case we use 9600, which is common baud rate used.

To do input, we should check the availability first. This is done by executing Serial.available(). Single character can be read by read() function. To send data to other end, Arduino provide print() and println() function. These functions are slight different where println() send extra new line character at the end of transmission.

We define three commands: ‘1’, ‘2’, and ‘3’. If we enter 1, we turn the LED on. If we enter 2, we turn the LED off. If we enter 3, we make the LED on/off in certain pattern.

Result

Once the program is uploaded, open “Serial Monitor” window in Arduino IDE. It is our tool to do serial communication using Arduino IDE. You can open it by navigate to Tool > Serial Monitor. Another way to do it is press CTRL + SHIFT + M at once.

SerialMonitor

To give input to Arduino, type in to the text box and click on “Send” button. Now give input of number 1, 2, 3 and see the result. You should see the LED are turned on and off according to your command.

Why it Works

It similar to project Simple Serial Communication where we received some commands from serial communication. This command are then processed to determine which routine should we do.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">