Send Commands from Host PC to Uno
Last updated May 2019
Introduction
Welcome to ProteShea – in this tutorial, we’ll send commands from the host PC to the Arduino Uno via UART. We interface a 10-segment LED bar graph and, based on the received data from the host, the Uno will display a unique binary number on the bar graph. This project is a good starting point for doing more complex commands on an embedded system from your PC, such as monitoring systems in a power plant.
Disclaimer
ProteShea, LLC is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Some links may be affiliate links, in which ProteShea, LLC earns a commission if you use that affiliate link. Please note that this is at no additional cost to you and helps us in creating more content.
Here’s what you’ll need to get started:
- FuelCan
- Modulus or solderless breadboard
- LED bar graph and the 470 ohm resistor array from the Modulus Kit
- Arduino Uno Rev3
- 8″ M/M jumpers
LED Bar Graph
Please see Arduino Uno Project 1 to learn more about the LED bar graph and internal circuitry.
Serial Terminal
Please see Arduino Uno Project 3 to learn more about the serial terminal.
Resistor Array
Please see Arduino Uno Project 1 to learn more about the 470 ohm resistor array.
Wiring
First, let’s place the LED bar graph onto the breadboard (if using a breadboard instead of Modulus). 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.
FuelCan Setup
I placed the breadboard in the bottom storage compartment to limit the length of the jumper wires and mounted the Arduino Uno to the FuelCan’s prototyping area. We need to supply GND to the 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 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.
For additional information about the Fuelcan-910, click here or download the user guide.
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.
//Send commands from Serial Monitor to Arduino Uno Rev3 | |
/*Copyright (c) 2019, ProteShea LLC | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
1. Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
2. Redistributions in binary form must reproduce the above copyright | |
notice, this list of conditions and the following disclaimer in the | |
documentation and/or other materials provided with the distribution. | |
3. Neither the name of the copyright holders nor the | |
names of its contributors may be used to endorse or promote products | |
derived from this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY | |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY | |
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
//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; | |
} | |
} | |
} |
About Author
Eric Shea is the founder of ProteShea and is an electrical engineer. He wishes to have a major impact on bridging the gap between engineering theory and real-world applications. He has worked at Kratos Defense, SpaceX, Air Force Research Laboratory, and Polaris Industries. He received a M.S. in electrical engineering from the University of Pittsburgh and a B.S. in electrical engineering from the University of Florida.