Project Description: Sending Hex values to an Arduino UNO
This simple tutorial will show you how to send Hexadecimal values from a computer to an Arduino Uno. The "Processing" programming language will be used to send the HEX values from the computer when a mouse button is pressed. The Arduino will use these values to adjust the brightness of an LED.
Learning Objectives
To Send Hexadecimal (Hex) values from a computer to the Arduino
Trigger an action based on the press of a mouse button
Learn to create a simple Computer to Arduino interface
Use Arduino's PWM capabilities to adjust brightness of an LED
/* ================================================================================================================================================== Project: 5 min tutorial: Send Hex from computer to Arduino Author: Scott C Created: 21th June 2015 Arduino IDE: 1.6.4 Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html Description: Arduino Sketch used to adjust the brightness of an LED based on the values received on the serial port. The LED needs to be connected to a PWM pin. In this sketch Pin 10 is used, however you could use Pin 3, 5, 6, 9, or 11 - if you are using an Arduino Uno. ===================================================================================================================================================== */
byte byteRead; //Variable used to store the byte received on the Serial Port int ledPin = 10; //LED is connected to Arduino Pin 10. This pin must be PWM capable.
voidsetup() { Serial.begin(9600); //Initialise Serial communication with the computer pinMode(ledPin, OUTPUT); //Set Pin 10 as an Output pin byteRead = 0; //Initialise the byteRead variable to zero. }
voidloop() { if(Serial.available()) { byteRead = Serial.read(); //Update the byteRead variable with the Hex value received on the Serial COM port. }
analogWrite(ledPin, byteRead); //Use PWM to adjust the brightness of the LED. Brightness is determined by the "byteRead" variable. }
Processing Sketch
The latest version of the Processing IDE can be downloaded here.
/* ================================================================================================================================================== Project: 5 min tutorial: Send Hex from computer to Arduino Author: Scott C Created: 21th June 2015 Processing IDE: 2.2.1 Website: http://arduinobasics.blogspot.com/p/arduino-basics-projects-page.html Description: Processing Sketch used to send HEX values from computer to Arduino when the mouse is pressed. The alternating values 0xFF and 0x00 are sent to the Arduino Uno to turn an LED on and off. You can send any HEX value from 0x00 to 0xFF. This sketch also shows how to convert Hex strings to Hex numbers. ===================================================================================================================================================== */
import processing.serial.*; //This import statement is required for Serial communication
Serial comPort; //comPort is used to write Hex values to the Arduino boolean toggle = false; //toggle variable is used to control which hex variable to send String zeroHex = "00"; //This "00" string will be converted to 0x00 and sent to Arduino to turn LED off. String FFHex = "FF"; //This "FF" string will be converted to 0xFF and sent to Arduino to turn LED on.
voidsetup(){ comPort = new Serial(this, Serial.list()[0], 9600); //initialise the COM port for serial communication at a baud rate of 9600. delay(2000); //this delay allows the com port to initialise properly before initiating any communication. background(0); //Start with a black background.
}
voiddraw(){ //the draw() function is necessary for the sketch to compile //do nothing here //even though it does nothing. }
voidmousePressed(){ //This function is called when the mouse is pressed within the Processing window. toggle = ! toggle; //The toggle variable will change back and forth between "true" and "false" if(toggle){ //If the toggle variable is TRUE, then send 0xFF to the Arduino comPort.write(unhex(FFHex)); //The unhex() function converts the "FF" string to 0xFF background(0,0,255); //Change the background colour to blue as a visual indication of a button press. } else { comPort.write(unhex(zeroHex)); //If the toggle variable is FALSE, then send 0x00 to the Arduino background(0); //Change the background colour to black as a visual indication of a button press. } }
The Video
The tutorial above is a quick demonstration of how to convert Hex strings on your computer and send them to an Arduino. The Arduino can use the values to change the brightness of an LED as shown in this tutorial, however you could use it to modify the speed of a motor, or to pass on commands to another module. Hopefully this short tutorial will help you with your project. Please let me know how it helped you in the comments below.
If you like this page, please do me a favour and show your appreciation :
Use a mouse to control LEDs attached to an Arduino. This project uses the processing language to transmit the mouse coordinates to the Arduino, which then uses this information to turn on some LEDs. Please see the video below to see it in action.
Components Required for this project:
Arduino UNO
Breadboard
9 LEDs
9 x 330 ohm resistors
Wires to connect the circuit
USB connection cable: to connect the computer to the Arduino
A computer: to run the processing sketch, and to compile / upload the Arduino sketch
/* This program was created by ScottC on 9/5/2012 to receive serial signals from a computer to turn on/off 1-9 LEDs */
void setup() { // initialize the digital pins as an output. pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); pinMode(9, OUTPUT); pinMode(10, OUTPUT); // Turn the Serial Protocol ON Serial.begin(9600); }
void loop() { byte byteRead;
/* check if data has been sent from the computer: */ if (Serial.available()) {
/* read the most recent byte */ byteRead = Serial.read(); //You have to subtract '0' from the read Byte to convert from text to a number. byteRead=byteRead-'0';
//Turn off all LEDS for(int i=2; i<11; i++){ digitalWrite(i, LOW); }
if(byteRead>0){ //Turn on the relevant LEDs for(int i=1; i<(byteRead+1); i++){ digitalWrite(i+1, HIGH); } } } }
//Open the serial port for communication with the Arduino //Make sure the COM port is correct myPort = new Serial(this, "COM6", 9600); myPort.bufferUntil('\n'); }
// Draw the Window on the computer screen void draw(){
// Fill canvas grey background( 100 );
// Set the stroke colour to white stroke(255);
// Draw a circle at the mouse location ellipse( nX, nY, 10, 10 );
//Draw Line from the top of the page to the bottom of the page //in line with the mouse. line(nX,0,nX,height); }
// Get the new mouse location and send it to the arduino void mouseMoved(){ nX = mouseX; nY = mouseY;
//map the mouse x coordinates to the LEDs on the Arduino. new_sX=(int)map(nX,0,800,0,10);
if(new_sX==old_sX){ //do nothing } else { //only send values to the Arduino when the new X coordinates are different. old_sX = new_sX; myPort.write(""+new_sX); } }