Temperature Visualization on Adafruit NeoPixel

This example shows how to use the MATLAB® Support Package for Arduino® Hardware to control an Adafruit NeoPixel RGB LED Strip to visualize current temperature using TMP36 temperature sensor.


Hardware Requirements

Hardware Setup

Define Configuration Variables

Define the minimum and maximum temperature to visualize on the NeoPixel strip. The minimum temperature corresponds to no pixels lit while maximum temperature corresponds to all 30 pixels lit, as shown in the following figure.

% Range of temperature (in celsius) to show on NeoPixel Strip
minTemp = 22;
maxTemp = 28;

Create NeoPixel Object

Create Arduino object

a = arduino('COM10', 'Uno', 'Libraries', 'Adafruit/NeoPixel')
a = 

  arduino with properties:

                    Port: 'COM10'
                   Board: 'Uno'
           AvailablePins: {'D2-D13', 'A0-A5'}
               Libraries: {'Adafruit/NeoPixel'}

Create NeoPixel object

% Using default brightness value
neostrip = addon(a, 'Adafruit/NeoPixel', 'D6', 30, 'NeoPixelType', 'GRB')
neostrip = 

  NeoPixel with properties:

               Pins: 'D6'
          NumPixels:  30
       NeoPixelType: 'GRB'
         Brightness:  0.250

Temperature-based Lighting

The temperature-based lighting does the following:

Create an animated line

The animatedline function allows you to plot the current temperature live.

h = animatedline('MaximumNumPoints', Inf);
ylim([minTemp, maxTemp]),
ylabel('Temperature (Celsius)');
title('TMP36 Temperature vs. Time-steps')

Define colors using colormap

The cool function in colormap represents gradual change in color from blue to violet. In this example, blue color would represent low temperature while the violet would reprensent a high temperature.

colors = cool(30);

Run the loop to read temperature and update the colors of the NeoPixel strip

The temperature is read from the TMP36 sensor connected to pin A5 on the arduino. The first line reads the raw analog input a value from this pin represented as value between 0V to 5V. This value is converted to temperature using the formula provided in the TMP36 datasheet.

$$temp = (volt - 0.5) * 100$$

This value is first clamped between minTemp and maxTemp defined above, and then scaled accordingly to determine the number of pixels that represent the measured temperature. Finally, the value is written to the NeoPixel strip and the realtime plot is updated.

for idx = 1:100
    % Read temperature
    temp = (readVoltage(a, 'A5') - 0.5) * 100;  % Formula adopted from TMP36 datasheet

    % Update the animatedline for plotting
    addpoints(h, idx, temp);

    % Clamp temperature
    if temp < minTemp
        temp = minTemp;
    elseif temp > maxTemp
        temp = maxTemp;

    % Compute number of pixels to turn on
    onPixels = round((temp - minTemp) / (maxTemp - minTemp) * neostrip.NumPixels);

    % Send command to NeoPixel Strip
    writeColor(neostrip, 1:neostrip.NumPixels, [colors(1:onPixels, :); zeros(neostrip.NumPixels - onPixels, 3)]);

    % Update plot

Clean Up

clear neostrip
clear a