Thursday 11 June 2015

Serial - Computer to Arduino

This is a two part project, in this first part we will use the serial "instruction" from a computer to turn on an LED.

In the second part we will use the Arduino to send and "instruction" to the computer. (coming in a few weeks)

* What you need *

 For this project you will need:
  1. Arduino UNO or NANO
  2. Your Computer with the Arduino IDE and a USB cable
  3. 2 x LED (two different colours)
  4. 2 x Resistor 150k Ohm
  5. Jumper wires
  6. Breadboard

 * The Project *

 OK so this is a nice and simple set up, first up lets get all the components on the board and wired up. Remember to check the polarity of the LED's and put a current limiting resister on each one.

 The Diagram:

Back ground information on Serial:

Serial is the process of sending on one bit of data at a time followed by the next bit, "bit" as in binary data (WIKI LINK).  Serial communications normally happens over two wires a TX (transmission) and RX (receive), on the Arduino UNO RX is pin 0 and TX is pin 1.

Bits are sent on at a time one after another and kept in track with the clock, a Baud rate of 9600 (commonly used all over and what I use for all my projects) which both devices are set to.

We are going to use it really simply to send a command from the computer via the USB cable, in to the USB to Serial chip (check out the postings on the UNO and NANO if you want to know what it is) and then to the Atmel Atmega chip which will control the processing and outputs.

The Code;

I have copied the code directly below, look out for the "// and the colour orange" and the words after this will be how I explain the parts of the code and what they do.

const unsigned int ledGREEN = 5;         // Naming digital pin 5 to be the Green LED
const unsigned int ledRED =7;            // Naming the digital pin 7 to be the Red LED
void setup() {
  pinMode(ledGREEN, OUTPUT);             // Setting "ledGREEN" to be an output
  pinMode(ledRED, OUTPUT);               // Setting "ledRED" to be an output
  Serial.begin(9600);                    // Tells the Arduino to start the Serial and at the speed 9600
}

void loop(){
  if (Serial.available() > 0) {          // Is the Serial ready? greater (>) than "0" or No
    int command = Serial.read();         // Setting a integer "command" to equal what serial "reads"
    if (command == '1')                  // If it reads "1" then complete what is in the below curly brackets
    {
      digitalWrite(ledGREEN, HIGH);      // Turn on Green LED
      digitalWrite(ledRED, LOW);         // Turn off Red LED
    }
    else if (command == '2')             // If it reads "2" then complete what is in the below curly brackets
    {
      digitalWrite(ledRED, HIGH);        // Turn on Red LED
      digitalWrite(ledGREEN, LOW);       // Turn off Green LED
    }
    else if (command == '3')             // If it reads "3" then complete what is in the below curly brackets
    {
      digitalWrite(ledGREEN, HIGH);      // Turn on Green LED
      digitalWrite(ledRED, HIGH);        // Turn on Red LED
    }
    else if (command == '4')             // If it reads "4" then complete what is in the below curly brackets
    {
      digitalWrite(ledGREEN, LOW);       // Turn off the Green LED
      digitalWrite(ledRED, LOW);         // Turn off the Red LED
    }
    }
  }

Instructions for use:

First up you will need to open up on the IDE the serial monitor, two ways of doing this;
1) Keyboard method - Press at the same time CTRL + SHIFT + M
2) Clicking - Click on tools, and then Serial Monitor

Now all you have to do is press a number and then press enter and watch the Arduino turn on and off the corresponding LED!

So if you type;

1 -  The green LED till turn on
2 - The red LED will turn on
3 - Both green and red LED will turn on
4 - Turn both LEDs off.




 

No comments:

Post a Comment