Starting Electronics Needs Your Help!
It is that time of the year when we need to pay for web hosting and buy new components and equipment for new tutorials. You can help by making a donation. Contribute to this website by clicking the Donate button. The total will be updated once daily. (You may need to clear your browser cache to see the updates.)

Target Amount: $2000
Amount Raised: $970

Donations Received

Top Donor: C.C. $100


Temperature Shield Software Sketches

Created on: 30 October 2012

Temperature Shield – Pages

A sketch must first be loaded to the Arduino that will send the temperature over the USB port and receive a byte from the USB port that tells it which LED to switch on.

A second sketch, written in the Processing language, must be run on the PC. This sketch will communicate with the Arduino over the USB port and display the temperature that the Arduino sends to it.

Arduino Sketch

Load this Arduino sketch to the Arduino board:

  Program:     temperature_shield2

  Description:  Sends the temperature out the USB port as a
                floating point string. The temperature is in
                degrees Celsius.
                Checks for characters on the USB port that
                will switch one of the 3 LEDs on.

  Date:         29 October 2012
  Author:       W.A. Smith,
int rx_byte;            // stores the byte received on the serial port
int red_LED = 6;        // LED pin numbers on Arduino
int grn_LED = 4;
int amb_LED = 2;

void setup()
    pinMode(red_LED, OUTPUT);      // LED pins
    pinMode(grn_LED, OUTPUT);
    pinMode(amb_LED, OUTPUT);
    digitalWrite(red_LED, LOW);    // switch LEDs off
    digitalWrite(grn_LED, LOW);
    digitalWrite(amb_LED, LOW);

void loop()
    float temperature = 0.0;   // stores the calculated temperature
    int sample;                // counts through ADC samples
    float ten_samples = 0.0;   // stores sum of 10 samples
    if (Serial.available() > 0) {
        rx_byte =;
        // received bytes that will switch one of the LEDs on
        if (rx_byte == 'R') {
            digitalWrite(red_LED, HIGH);
        else {
            digitalWrite(red_LED, LOW);
        if (rx_byte == 'G') {
            digitalWrite(grn_LED, HIGH);
        else {
            digitalWrite(grn_LED, LOW);
        if (rx_byte == 'A') {
            digitalWrite(amb_LED, HIGH);
        else {
            digitalWrite(amb_LED, LOW);
    // read the temperature from A0 and convert it into a floating point number
    // send the temperature over the USB port after conversion
    else {
        // take 10 samples from the MCP9700
        for (sample = 0; sample < 10; sample++) {
            // convert A0 value to temperature
            temperature = ((float)analogRead(A0) * 5.0 / 1024.0) - 0.5;
            temperature = temperature / 0.01;
            // sample every 0.1 seconds
            // sum of all samples
            ten_samples = ten_samples + temperature;
        // get the average value of 10 temperatures
        temperature = ten_samples / 10.0;
        // send temperature out of serial port
        ten_samples = 0.0;

Processing Sketch

Download the temperature_shield Processing code and open it in the processing IDE.

Make sure that the Arduino is running the temperature_shield2 sketch listed above and also make sure that the Arduino is plugged into the PC.

Run the Processing temperature_shield sketch on the PC and check in the bottom output window of the IDE for the available serial ports which will be listed there.

Modify the code if necessary to select the correct serial port that the Arduino is connected to. This is the line of code to modify:

ser_port = new Serial(this, Serial.list()[0], 9600);

If your Arduino serial port was listed next to [2], then change the above line to:

ser_port = new Serial(this, Serial.list()[2], 9600);

After changing the serial port if necessary, run the sketch from the Processing IDE and you should see the current temperature and temperature history graph displayed in a window.

The temperature ranges where the LEDs will switch on can be changed in the sketch here:

// upper and lower temperature ranges on which to switch on LEDs
int red_led_hi = 100;
int red_led_lo = 40;
int grn_led_hi = 39;
int grn_led_lo = 26;
int amb_led_hi = 25;
int amb_led_lo = 20;

This video shows how to change the serial port in the code and then shows the application measuring temperature.

Can't see the video? View on YouTube →

← Go back to Part 2 of 3Go to Part 1 of 3 →