.
Send HEX values to Arduino Reviewed by Unknown on 13:52 Rating: 4.5

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.



 

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
  • Learn to use Arduino's analogWrite() function
  • Create a simple LED circuit


 

Parts Required:


Fritzing Sketch


The diagram below will show you how to connect an LED to Digital Pin 10 on the Arduino.
Don't forget the 330 ohm resistor !
 


 
 

Arduino Sketch


The latest version of Arduino IDE can be downloaded here.
 
  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* ==================================================================================================================================================
         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.

void setup() {
 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.
}

void loop() {
  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.
 
  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* ==================================================================================================================================================
         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.

void setup(){
    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.
    
}


void draw(){ //the draw() function is necessary for the sketch to compile
    //do nothing here //even though it does nothing.
}


void mousePressed(){ //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 :

 
Visit my ArduinoBasics Google + page.
Follow me on Twitter by looking for ScottC @ArduinoBasics.
I can also be found on Pinterest and Instagram.
Have a look at my videos on my YouTube channel.


 
 
             


 
 



However, if you do not have a google profile...
Feel free to share this page with your friends in any way you see fit.




Description: Send HEX values to Arduino Rating: 3.5 Reviewer: Unknown ItemReviewed: Send HEX values to Arduino
Arduino UNO: LED Sensor, Part Two Reviewed by Unknown on 23:19 Rating: 4.5

Arduino UNO: LED Sensor, Part Two




Building on the last project, I am now using a Red and a Yellow LED as a Sensor to detect light coming from an RGB LED.

Putting different coloured Mega Bloks over the LEDs has different effects on the Sensors as the RGB LED gets brighter and brighter.

I used the Processing Language to control the brightness of the RGB LED through a Serial command, and then use the resulting Sensor readings from the Yellow and the Red LEDs to create a chart or plot.

Here are the results of my experiment.

Red Mega Blok




Yellow Mega Blok



Green Mega Blok













When the displayed bars are RED, it indicates that the Red LED is absorbing MORE light than the Yellow LED (and vice versa). Hence this is a "Difference Chart".
The Green Mega Blok absorbs more Red Light than the other blocks, therefore producing a big difference between Red LED sensor readings and Yellow Sensor readings.

Here is the list of components required to perform this experiment
All parts hardware parts except for the wires and the Mega Bloks
can be found in the Sparkfun Inventors Kit.

Here is the Sketch: (created in Fritzing):

































Here is the Arduino Code:

arduino code RedYellow Sensor with RGB LED

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//Define the pins for the Red LED Sensor
#define Red_LED_Sensor_POS 4
#define Red_LED_Sensor_NEG 5

//Define the pins for the Yellow LED Sensor
#define Yellow_LED_Sensor_POS 7
#define Yellow_LED_Sensor_NEG 8

//Define the pin for the RGB LED torch
#define RGB_LED_RedPin 9
#define RGB_LED_GreenPin 10
#define RGB_LED_BluePin 11
int intensity=0;


//Define the maximum cycles/time allowed for each LED to capture light
long max_darkness=80000;


void setup(){
//Setup the RED LED Sensor
pinMode(Red_LED_Sensor_POS,OUTPUT);
digitalWrite(Red_LED_Sensor_POS,LOW);

//Setup the YELLOW LED Sensor
pinMode(Yellow_LED_Sensor_POS,OUTPUT);
digitalWrite(Yellow_LED_Sensor_POS,LOW);

//No need to setup the RGB LED Pins

//Turn on Serial Protocol
Serial.begin(9600);
}

void loop()
{

byte byteRead;
// check if data has been sent from the computer:
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255):
byteRead = Serial.read();
// set the brightness of the LED:
analogWrite(RGB_LED_RedPin, byteRead);
analogWrite(RGB_LED_GreenPin, byteRead);
analogWrite(RGB_LED_BluePin, byteRead);

//Read the amount of Yellow light
read_LED('Y', Yellow_LED_Sensor_NEG);

//Read the amount of Red light
read_LED('R', Red_LED_Sensor_NEG);
}
}

void read_LED(char LED_Colour, int LED_Pin){

// Charge the LED by applying voltage in the opposite direction
pinMode(LED_Pin,OUTPUT);
digitalWrite(LED_Pin,HIGH);

//Read the amount of Light coming into the LED sensor
long darkness=0;
int lightLevel=0;
pinMode(LED_Pin,INPUT);
digitalWrite(LED_Pin,LOW);

while((digitalRead(LED_Pin)!=0) && darkness < max_darkness){
darkness++;
}

lightLevel=((max_darkness-darkness)+1)/80;

//Print the LED colour
Serial.println(LED_Colour);
//Print the light level
Serial.println(lightLevel);
}




Here is the Processing Code:

processing code Read Serial Chart and Write Serial

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Processing code for this example */

// Graphing sketch

//This sketch was written by ScottC, but was adapted from a sketch
//written by Tom Igoe in 2005

// This example code is in the public domain.

import processing.serial.*;

Serial myPort; // The serial port
int xPos = 1; // horizontal position of the graph
float YellowVal=0; // The variable to hold the Yellow Sensor Reading
float RedVal=0; // The variable to hold the Red Sensor Reading
float Diff=0; // The variable to hold the difference between the readings
int Switcher=0; // Used to control the flow of the program

void setup () {
// set the window size:
size(1020, 750);

// List all the available serial ports
println(Serial.list());
// I use COM13 for my Serial Port - you will need to change this to suit your system
myPort = new Serial(this, "COM13", 9600);
// don't generate a serialEvent() unless you get a newline character:
myPort.bufferUntil('\\n');
// set inital background:
background(0);
//Send a value to the Arduino to start the feedback mechanism
myPort.write(0);
}
void draw () {
// everything happens in the serialEvent()
}

void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil('\\n');

if (inString != null) {
// trim off any whitespace:
inString = trim(inString);

//The arduino sends 2 sensor readings. The following code
//helps to identify which reading is which.
if(inString.equals("Y")){
Switcher=0;
} else if (inString.equals("R")){
Switcher=1;
} else {

//Convert the String to a float
float inByte = float(inString);
//Map the reading, so that the chart fits within the window.
inByte = map(inByte, 0, 1000, 0, height);

if(Switcher==0){
//Save the reading from the yellow sensor to YellowVal
YellowVal=inByte;
} else {
//Save the reading from the red sensor to RedVal
RedVal=inByte;
//Calculate the difference between the readings
Diff=RedVal-YellowVal;

//If the yellow sensor is greater, plot with a yellow line
//If the red sensor reading is greater, plot a red line.
if(Diff<=0){
stroke(255,255,0);
Diff=abs(Diff);
} else {
stroke(255,0,0);
}
// draw the line:
line(xPos, height, xPos, height - Diff);

// at the edge of the screen, go back to the beginning:
if (xPos > width) {
xPos = 0;
background(0);
//Send a value to the Arduino to change the intensity
//of the RGB LED and take another reading
myPort.write(xPos);
} else {
// increment the horizontal position: Increment by more
// to get less readings and to make it quicker
xPos+=4;
if (xPos>0){
//Send a value to the Arduino to change the intensity
//of the RGB LED and take another reading
myPort.write(xPos/4);
} else {
myPort.write(xPos);
}
}
}
}
}
}
Description: Arduino UNO: LED Sensor, Part Two Rating: 3.5 Reviewer: Unknown ItemReviewed: Arduino UNO: LED Sensor, Part Two
Back to top