You may also read: Wireless Control of LED with Arduino and Smartphone
Mostly, alphabet/numeric keys from the computer keyboard are
used to give commands to the Arduino to control the output pins through the
serial communication.
Let’s get started with the Arduino serial communication
project.
Things you will need:
1. Arduino Uno
2. Breadboard
3. Arduino Cable
4. LED 5V (red, yellow or green)
5. Jumper cables
6. Resistor – 220 ohm
Step 1: Making the Circuit
You may connect the LED or skip this step as the Arduino Uno R3
(a common development board) comes with inbuilt LED at pin 13. But, we shall
make the circuit so as to understand the pins connections and get hands-on
experience.
1. Fix the LED on the breadboard such that the two legs of the
LED do not short.
2. Connect the pin 13 of the Arduino to the positive terminal
(the longer leg) of the LED with jumper cable.
3. Connect the Gnd of the Arduino to the negative terminal (the
shorter leg) of the LED with jumper cable.
4. Connect the 220 ohm resistor between pin 13 and the positive
of the LED so as to protect the LED from getting damaged.
Step 2: Uploading the Code
Enter the following code into the Arduino IDE or paste it. The
code would be called by the Arduino to check whether the value entered by us
matches with the variable in the code. If it does, the corresponding operation would be performed.
int led = 13; // Pin 13
void setup()
{
pinMode(led, OUTPUT); // Set pin 13 as digital out
// Start
up serial connection
Serial.begin(9600); // baud rate
Serial.flush();
}
void loop()
{
String
input = "";
// Read
any serial input
while
(Serial.available() > 0)
{
input
+= (char) Serial.read(); // Read in one char at a time
delay(5); // Delay for 5 ms so the next char has time to be received
}
if (input
== "on")
{
digitalWrite(led, HIGH); // on
}
else if
(input == "off")
{
digitalWrite(led, LOW); // off
}
}
Now, connect the Arduino to the computer via the USB cable
provided and choose the correct COM port.
Click on the upload button from the toolbar. And, wait till the software
says “Done Uploading”.
Step 3: LED Control from Computer
Once the code is uploaded, it is time to check the LED output
as we desire it to be. If we command it to turn on, it should be turned on and
if we command it to turn off, it should be turned off.
Open the serial monitor from the Arduino software (Tools>Serial Monitor), it may
appear in a new window. You may also open it by using commands : Ctrl+Shift+M. The serial monitor would be initially blank and has an input box on the top.
Enter the term “on” and hit enter.
Whoa! Did you see that? The LED just turned on immediately
without delay.
Now, enter the term “off” and hit enter.
Oh, now the LED just turned off. So you get the idea that you
may control the output pins from your computer and Arduino works as you wish.
Congratulations! You have just completed your Arduino Project
– simple, fun and easy.
Add another commands for example “yes”, “no” etc or even
command multiple LED with just a little update in the code.
You may also read: Wireless Control of LED with Arduino and Smartphone
Stay Hungry! Stay Foolish!
0 comments:
Post a Comment