Project tutorial
Sending Commands from Host to Uno

Sending Commands from Host to Uno © GPL3+

Display an output on the LED bar graph based on the serial command received from the host.

  • 687 views
  • 0 comments
  • 4 respects

Components and supplies

Necessary tools and machines

Apps and online services

About this project

Introduction

If you haven’t read our Getting Started Guide or Project 3for the Arduino Uno Rev3(Uno), please read those first. Otherwise, continue reading. In the last tutorial, we sent data from the Uno to the host and in this tutorial, we’ll be sending data from the host to the Uno. We interface a 10-segment LED bar graph and communicate with the host via UART. Based on the received data from the host, the Uno will display a certain binary number on the bar graph. We’ll use 4/10 LEDs on the bar graph and each LED input will be an output from the Uno.

LED Bar Graph

Please see Arduino Uno Rev3 Project 1 to learn more about the LED bar graph and internal circuitry.

Serial Terminal

Please see Arduino Uno Rev3 Project 3 to learn more about the serial terminal.

Resistor Array

Please see Arduino Uno Rev3 Project 1 to learn more about the 470 ohm resistor array.

Wiring

I’m using a breadboard instead of Modulus since almost everybody has a breadboard. First, let’s place the LED bar graph onto the breadboard. Insert both so that the body is over the valley of the breadboard. You do not want the pins to short each other by being connected to the same node. Next, place the 470 ohm resistor array on the cathode side of the LED and tie pin 1 of the array to GND. Make sure that pin 1 of the resistor array is not connected to any of the cathodes of the LED bar graph.

Next, use a M/M jumper to wire the anode side of the LED bar graph as outputs to Uno pins 2-5. For example, pin 10 of the bar graph will get wired to Uno pin 5, pin 9 of the LED will get wired to Uno pin 4, and so on. This is shown in the image below.

If you haven’t mounted the Uno onto the prototyping area of the FuelCan, go ahead and do that. I placed the breadboard in the bottom storage compartment to limit the length of the jumper wires. We need to supply GND to the and ground rail on the breadboard by using the provided banana jack to test-lead clip cable to do so. You will need one male header pin to mount the test-lead clip to on the breadboard side. Plug the Type A side of the USB cable into the USB1 receptacle and the Type B side into the Uno’s receptacle. Then, plug in the Type A to Type A USB cable into a USB port on your computer and the external USB connector of the FuelCan. Power up the FuelCan with the AC-DC power adapter.

Software

Once the wiring is complete and the FuelCan is powered up, we can now load the sketch onto the Uno. The sketch is below. Open up the Serial Monitor within the IDE. Notice in the code that there is a switch statement to choose the correct LED output based on the input received from the host. For example, if we send a 1 to the Uno, the code inside of case 1 will execute.

There are cases 0-9 so send each number to the Uno and look at the output of the LED bar graph. It should be displaying the binary equivalent of the number received. There is also a line in the code, Serial.print(RXdata), which sends what the Uno receives back to the host. This is a good debugging technique to use since you can verify what data is being transmitted and received. When you send a ‘1’ to the Uno, notice the data received back on the Serial Monitor – it shows 4910. Why? The data is converted to ASCII. The ASCII equivalent of 1 is 49 and the ASCII equivalent of ‘New Line’ is 10.

Code

Sending Commands from Host to UnoArduino
//Send commands from Serial Monitor to Arduino Uno Rev3

//variable declarations
int RXdata = 0; 

void setup() {
  // put your setup code here, to run once:
  //set pins 2-5 as output for LEDs
  for (int i = 2; i < 6; i++){
    pinMode(i, OUTPUT);
  }

  Serial.begin(9600);   //configure serial port to 9600 baud
}

void loop() {
  // put your main code here, to run repeatedly:

  if(Serial.available() > 0){   //check if any data was received
    RXdata = Serial.read();
    Serial.print(RXdata, DEC);

    switch(RXdata){
      case '0':
        //set LEDs to 0000
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
        digitalWrite(4, LOW);
        digitalWrite(5, LOW);
        break;
      case '1':
        //set LEDs to 0001
        digitalWrite(2, HIGH);
        digitalWrite(3, LOW);
        digitalWrite(4, LOW);
        digitalWrite(5, LOW);
        break;
      case '2':
        //set LEDs to 0010
        digitalWrite(2, LOW);
        digitalWrite(3, HIGH);
        digitalWrite(4, LOW);
        digitalWrite(5, LOW);
        break;
      case '3':
        //set LEDs to 0011
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        digitalWrite(4, LOW);
        digitalWrite(5, LOW);
        break;
      case '4':
        //set LEDs to 0100
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
        digitalWrite(4, HIGH);
        digitalWrite(5, LOW);
        break;
      case '5':
        //set LEDs to 0101
        digitalWrite(2, HIGH);
        digitalWrite(3, LOW);
        digitalWrite(4, HIGH);
        digitalWrite(5, LOW);
        break;
      case '6':
        //set LEDs to 0110
        digitalWrite(2, LOW);
        digitalWrite(3, HIGH);
        digitalWrite(4, HIGH);
        digitalWrite(5, LOW);
        break;
      case '7':
        //set LEDs to 0111
        digitalWrite(2, HIGH);
        digitalWrite(3, HIGH);
        digitalWrite(4, HIGH);
        digitalWrite(5, LOW);
        break;
      case '8':
        //set LEDs to 1000
        digitalWrite(2, LOW);
        digitalWrite(3, LOW);
        digitalWrite(4, LOW);
        digitalWrite(5, HIGH);
        break;
      case '9':
        //set LEDs to 1001
        digitalWrite(2, HIGH);
        digitalWrite(3, LOW);
        digitalWrite(4, LOW);
        digitalWrite(5, HIGH);
        break;
    }
  }

}

Schematics

Circuit Schematic
Schematic v2 jis5z4qz1j

Comments

Similar projects you might like

Sending Data from PC to Another Using Arduino UNO

Project tutorial by Mohamed Maher

  • 3,365 views
  • 2 comments
  • 10 respects

Simple UNO calculator

Project tutorial by Joprp05

  • 9,608 views
  • 2 comments
  • 11 respects

Simple Arduino Uno - ESP 8266 Integration

Project tutorial by circuito.io team

  • 105,939 views
  • 24 comments
  • 86 respects

Controlling an LED Matrix with Arduino Uno

Project tutorial by Igor Fonseca Albuquerque

  • 34,147 views
  • 5 comments
  • 28 respects
Add projectSign up / Login