IxD IoT

working with the arduino for IoT


Working with Sensors


In this tutorial we are going to work with a simple sensor - a light sensor or photoresistor. All it does is change the flow of current based on the amount of light shining on it. It's kind of useful for something like monitoring how much light a plant gets throughout the day.

The main thing we want to learn from this tutorial is how to see the values that we get from a sensor. We are going to do this using the serial port (USB) on our computer. There is an application built into the arduino IDE that allows us to read what's being sent to this port.

Photoresistor Arduino wiring for a photoresistor

For this application you will need:

  1. A photoresistor
  2. A 220 ohm resistor
  3. Some jumper wires

Step 1: Set up power rails

The wiring is a little more complex for this application. The first thing we are going to do is set up power rails for our breadboard. Whenever we want to use power or ground then we can connect anywhere along these rails.

  • Connect GND to one of the rails on the side - it should have a '-' marking to indicate negative.
  • Connect 5V to the '+' rail on the side

Step 2: Connect the photoresistor

Now we want to connect up our photoresistor. To do this:

  • Connect the positive power rail to a row on the breadboard
  • Plug one end of the photoresistor into that positive rail and the other into a vacant row
  • Plug one end of the resistor into that row and the other end into a vacant row
  • Connect that final row into the negative power rail
  • Connect the row with both the photoresistor and the resistor to PIN A0 of your arduino

As the light shining onto the photoresistor changes intensity, it changes the flow of current along the circuit. The wire we have plugged into PIN A0 allows us to read the amount of that current. This gives us a measure of the amount of light shining.


Step 3: Coding

The main thing that we want here is to get the value of that reading so we have a quantity of light that we can use. Here's the code that you will need:

int sensorPin = A0;

int sensorValue = 0;


void setup()

{

Serial.begin(9600);

}


void loop()

{

// read the value from the sensor:

sensorValue = analogRead(sensorPin);

// send this value to the serial port

Serial.println(sensorValue);

delay(5000);

}


The code Serial.begin(9600) opens the serial port (USB) so that information can be sent through that port. The value 9600 is just the speed for that communication.

Once you have uploaded this code to your arduino you can use the IDE to see the information that is being sent through the serial port by clicking on the small magnifying glass in the top right corner of the IDE. What you should see is a series of values. If you cover the light sensor you will see the values change.

Photoresistor

Step 4: Using the values from the sensor


Now, we want to use those values from the sensor to do something (after all, that's the point of the Internet of Things). What we will do is use those values to make the LED flash at different speeds.

To do this, you'll need to go back to the blink tutorial and add the external LED back to our circuit. No diagrams for this one, you're on your own. But feel free to ask for help from a tutor.

Here's what your code will change to:


int sensorPin = A0;

int sensorValue = 0;

int ledPin = 13;


void setup()

{

// declare the ledPin as an OUTPUT:

pinMode(ledPin, OUTPUT);

Serial.begin(9600);

}


void loop()

{

// read the value from the sensor:

sensorValue = analogRead(sensorPin);

// send this value to the serial port

Serial.println(sensorValue);

digitalWrite(ledPin, HIGH);

// the delay is related to the light reading

delay(sensorValue);

digitalWrite(ledPin, LOW);

delay(sensorValue);

}


There is one major change when compared to the code we used for Blink. Instead of using LED_BUILTIN to control the LED, we defined a new variable at the start of our code: int ledPin = 13. This is effectively the same thing. However, by looking at your arduino you can see there are a lot of pins that we can use. This is how you can use different pins for different things - just by referring to the number.

Now, the LED will blink at different rates based on the light reading. We did this by making the delay change to the sensorValue. So, if the light reading is 847, the delay will be 847 milliseconds. If the reading changes to 186 then the delay will be 186 milliseconds. The less light that reaches the photoresistor, the faster the LED will blink.


Neopixels

Whilst LEDs are fun, neopixels are much more fun.

What's special about neopixels is that you can have long strips of them all controlled from only three wires connected to your arduino. Plus the colours of each pixel can be changed individually. Arduino life is much better with neopixels.


Step 1: Getting the neopixel library

A library is essentially a program that contains a number of functions written by somebody else. Rather than you having to create your own code, you can use code created by someone else and only use a few commands to do it.

To import the neopixel library we need to use the arduino library manager.


Photoresistor Click on Search in the menu, click on Include Library then check to see if Adafruit Neopixel is already listed. If it isn't you need to add it in. Arduino wiring for a photoresistor To install the library, click on Manage Libraries.... A new box will appear. In the search bar enter neopixel and look for the entry for Adafruit Neopixel by Adafruit. Click on that option and then on install.

Step 2: Adding the library to your code

Now you can add the library into your code (called a sketch). To do this:

  • Move your cursor to the top of your sketch
  • Click on Sketch in the menu
  • Click on Include Library
  • Click on the entry for Adafruit Neopixel

Photoresistor

After you have done this, you should see a new line added to the top of your code: #include <Adafruit_NeoPixel.h>. This makes available to you all the functions you need to get neopixels running easily for you.


Step 3: Wiring up your circuit

For this, you will need to plug your strip into the breadboard.

There are three pins. If you look closely on the strip One needs to go to the positive rail, one to the ground rail and one connects to PIN 6.

Photoresistor

Plug your strip into the breadboard as in the above picture. Each pin of the neopixel strip should be in a different row.

  • Left pin connects to the ground rail
  • Middle pin connects to Arduino PIN 6
  • Right pin connects to power (positive) rail

Step 4: Code

Now we need to change our code. We want to change the number of pixels that light up and have them display different colours based on the light reading. The darker the light is, the more neopixels we want to light up.

This code is a little more complex that other code we have used. We are introducing if else statements and for loops. If these aren't making sense, just grab someone to help you out.

Here's your code

#include <Adafruit_NeoPixel.h>


#ifdef __AVR__

#include <avr/power.h>

#endif


#define PIN 6

#define NUMPIXELS 3


Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);


int sensorPin = A0;

int sensorValue = 0;


// we use this just for effect

int delayval = 100;


void setup()

{

pixels.begin();

Serial.begin(9600);

}


void loop()

{

int litPixels = 0;


// turn off pixels

for (int i=NUMPIXELS-1;i>=0;i--)

{

pixels.setPixelColor(i, pixels.Color(0,0,0));

pixels.show();

delay(delayval);

}


// read the value from the sensor:

sensorValue = analogRead(sensorPin);


// determine how many pixels to turn on

if (sensorValue < 300)

{

litPixels = 1;

}

else if (sensorValue < 600)

{

litPixels = 2;

}

else

{

litPixels = 3;

}


// turn on pixels with different colours

for (int i=0;i<litPixels;i++)

{

if (i==0)

{

pixels.setPixelColor(i, pixels.Color(0,0,150));

}

else if (i==1)

{

pixels.setPixelColor(i, pixels.Color(0,150,0));

}

else if (i==2)

{

pixels.setPixelColor(i, pixels.Color(150,0,0));

}

pixels.show();

delay(delayval);

}


// send reading to serial after converting to a string

Serial.println(sensorValue);


delay(30000);

}


There's a few changes to your previous code. We will talk you through them.

The main thing that you will need to play with is the values in the statements that look like if (sensorValue < 200) as they will depend on your specific photoresistor.

Plug in your arduino, upload the code and see what happens. Play around with the numbers referred to above. Cover the photoresistor with varying levels of shade or expose it to greater light and see what happens.