Pages

22 June 2015

Send HEX values to Arduino

FIVE MINUTE TUTORIAL

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.

Arduino Sketch

/* ================================================================================================================================================== 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 Portint ledPin = 10; //LED is connected to Arduino Pin 10. This pin must be PWM capable. voidsetup() {
Serial.begin(9600); //Initialise Serial communication with the computerpinMode(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

/* ================================================================================================================================================== 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 Arduinoboolean 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 0xFFbackground(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 Arduinobackground(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 :

3 comments:

Great explanation, very clear. I have done something similar with a RGB LED strip, HC 06 bluetooth serial module and an android app to choose any RGB colour with colour wheel or sliders, transmit it in HEX and display it. I also added a colour sensor that I found on instructables, so that I could scan colours and have them displayed on the phone’s screen.