Today I will show you how to capture information about the environment with sensors. In this lab, I will be detecting the temperature and amount of light, but the same approach can be used with a wide range of sensors.
In previous videos I have demonstrated how a microcontroller, like an Arduino, can be used to control things. That’s an important part of the Internet of Things, but there is another, as important part that is about capturing information about the environment. The components that are used to do this are called sensors, and the name correctly leads the thoughts to our human senses, because they can be regarded as digital senses. There are many different sensors, and they can sense things like temperature, light, humidity, magnetism, tilt, rotation, sound, touch, shock, obstacle, knock, proximity, flame, pulse, and more.
Here you can see the needed hardware, and in addition to the Arduino, the important components are the two sensors – one to capture the temperature and another to capture the amount of light, known as a photocell or photo resistor. To connect everything, you need a breadboard and eight jumper cables that have a (male) pin on each end.
To setup the hardware, you start by disconnecting everything from its power source, as usual. First you connect the Arduino ground (GND) to the ground (minus) on the breadboard, and then connect the 5V on the Arduino to the power (plus) on the breadboard. Connect the two sensors to the breadboard as shown here, and then connect both power and ground to the respective pins on each sensor. The pins labeled “S” is where the sensors sends out its signal, and as they are analog signals, we connect them to the analog ports (that begins with A). Connect the signal of the temperature sensor to A0 and the signal of the light sensor to A1.
Here is the Arduino code (sketch)…
const int tempPin = 0;
const int lightPin = 1;
int value = 0;
float temp = 0.0;
value = analogRead(tempPin);
temp = log((10240000.0 / value) - 10000.0);
temp = 1.0 / (0.001129148 + (0.000234125 + (0.0000000876741 * temp * temp)) * temp);
temp = temp - 273.15;
Console.print(temp); Console.print(","); // Celsius
temp = (temp * 9.0) / 5.0 + 32.0;
Console.print(temp); Console.print(","); // Fahrenheit
value = analogRead(lightPin);
Console.println(value); // Light
…and after the initial include, constants for the two analogue input pins are defined (on lines 3-4), and two variables are prepared (on lines 5-6). In the setup, we initialize the bridge (on line 10), which allows communication between the microcontroller (32U4) and the microprocessor (AR9331, with Linux), and then we set up the console (on lines 11-13), so that we can see the values read. The eternal loop starts by reading the value from the temperature sensor, and since that value is a raw value (in the range 0-1023) it needs to be converted (on lines 19-21 and 23) to a temperature using a formula (it’s called the Steinhart-Hart Thermistor Equation, and you can read more about it on Wikipedia, http://en.wikipedia.org/wiki/Thermistor). The first temperature logged (on line 22) is in Celsius, and the second (on line 24) is in Fahrenheit. The light sensor value is read (on line 26) and logged (on line 27), and then the loop halts for a second (on line 28) to calm the feed of values.
Please see my previous videos on how to load the code (sketch) onto the Arduino, and to see the console log, you need to open a terminal window and give the commands (replace 0.0.0.0 with the IP address of the Arduino Yún, and you find it in the Tools, Port menu in the Arduino IDE):
telnet localhost 6571
Using this approach you can capture information about the environment with a wide range of sensors.