Project

Communicating Serially with the Arduino

June 01, 2015 by Tim Youngblood

Communicating serially with the Arduino is easy! Here, we'll show how to take some temperature measurements and then send the measurements over the serial link.

Communicating serially with the Arduino is really easy. Here, we'll show how to take some temperature measurements and then send the measurements over the serial link.

Unlike other devices, Arduino serial communications is simple to use. The Arduino programming environment has a feature, called the serial monitor, which is specifically for viewing the serial data communication. To activate it:

  1. Go to toolbar
  2. Click on Serial monitor option 
  3. Select the baud rate specified the Serial.begin() function.

Here are some Arduino serial communications functions.


Serial.begin(speed) 

This function sets the serial communications speed. It has one parameter, speed, which is usually set to 9600.


Serial.read()

This function receives data from the serial port.


Serial.write(val)

This function sends data via the serial port. The parameter val can be a single variable, a string or an array.


Serial.printIn(val, format)

This functions prints val to the Arduino IDE serial monitor using some specific format. 


Making temperature measurements with an LM35

The LM35 is an ideal temperature sensor for measuring ambient temperature. Different versions of the device are shown below.

Connection Diagram
Connection Diagram

The LM35 provides a linear output proportional to the temperature, with 0 V corresponding to 0 degrees C and an output voltage change of 10 mV for each degree C change. LM35s are easier to use than thermistors and thermocouples because they are so linear and require no signal conditioning. 

The output of an LM35 can be connected directly to a Arduino analog input. Because the Arduino analog-to-digital converter (ADC) has a resolution of 1024 bits, and the reference voltage is 5 V, the equation used to calculate the temperature from the ADC value is:

temp = ((5.0 * analogRead(pin)) / 1024) * 100.0

Experiment: 


Hardware Required

  • 1 x LM35 temperature sensor
  • 4 x LEDs
  • 2 X 220 Ohm resistors 
  • 1 x Arduino Mega2560
  • 1 x breadboard
  • 10 x jumper wires

Circuit Diagram
Circuit Diagram

Connect the components as shown in the circuit diagram above:

The sensor will be powered by the 5V and GND pins from the Arduino. The sensor output connects to Arduino pin A0, and when the code reads the value of this analog voltage using the function analogRead(0), it returns a value from 0 to 1023. The program will then calculate the temperature using the formula:

   

temp = ((5.0 * analogRead(pin)) / 1024) * 100.0

or

temp =  analogRead(pin) * 0.48828125;

Finally, the temperature reading will be written to the IDE serial monitor with the function Serial.print(). A typical display will look like the screenshot below:

Output
Output


Code 

The code for this experiment is shown below. In addition to displaying the temperature on the IDE serial monitor, the program will light a yellow LED if the measured temperature is about 70 degrees C or a green light if it is below 70 degrees C. This is done using if-else condition statements and the digitalWrite(pin,status) function.  

const int adc = 0 ; 		//naming pin 0 of analog input side as adc
const int high = 8 ; 		// For turning on and off yellow LED
const int low  = 9 ; 		// For turning on and off Green LED

void setup()
{
 Serial.begin(9600) ;  		//Starting serial Communication at baud rate of 9600
                                    		  
 pinMode(high,OUTPUT); 	//declaring LED pins as OUTPUT 
 pinMode(low,OUTPUT); 
}

void loop()
{

  int adc  = analogRead(0) ;    		 //reading analog voltage and storing it in an integer 
  adc = adc * 0.48828125;        		//converting reading into Celsius 
  Serial.print("TEMPRATURE = "); 	//to Display on serial monitor  
  Serial.print(adc);            		//this will show the actualtemp
  Serial.print("*C");          		 //TEMPRATURE = 27*C ETC
  Serial.println();              			//To end the line  
  delay(1000);                   		//1 Sec delay
  
  /*
  if (temperature (adc) > 70 ° C )
          turn on Yellow Leds 
          turn off Green Leds
  else
        turn off Yellow Leds
        turn on Green Led
  */
  
  if(adc>70) 				 // This is the control statement 
  {
    digitalWrite(high,HIGH) ; 
    digitalWrite(low,LOW) ; 
  }
   else
   {
    digitalWrite(high,LOW) ; 
    digitalWrite(low,HIGH) ; 
   }
}

communicating_serially.zip

Video