Reading Analog Sensors with an Arduino

Arduino Analog Sensor Featured

02: Reading Analog Sensors with an Arduino

Learning Outcomes:

One of the great things about the Arduino microcontroller is that it can easily be made to react to its surroundings by using various different types of sensors. In this tutorial we will be focusing on using an analog light sensor to turn on an LED when it is dark and to turn it off when it is light. The tutorial is meant to teach you how to use an Arduino to read various analog sensor outputs and to then get your project to react to these inputs. The same basics instructions can be used for most analog sensors although some will need more tweaking if you want accurate measurements of the surroundings. Components required for this tutorial: 1 Arduino (Any Arduino will do) 1 USB cable 1 Analog sensor (in this case we are using a light sensor) 1 LED 1 Resistor (220-ohm) 1 Breadboard Assorted Jumper wires Instructions: Step 1: First we need to connect all of the components up to the Arduino using the jumper wires and the breadboard. You can just follow the included Fritzing Diagram or one of the photos of the circuit. The code for this tutorial has been written with the analog sensor on pin A0 of the Arduino and the LED on pin 6. Arduino Analog Sensor WiredArduino Analog Sensor Wired Fritzig Step 2: If you have not already done so download and install the Arduino IDE from here. Then open the Arduino IDE and go to File–>Examples–>Basics–>AnalogReadSerial, this will load an example sketch that can be used to read most analog sensors. Arduino Analog 1Connect the Arduino to your computer using the USB cable and upload the code to the Arduino using the arrow at the top that points to the right (circled red in the picture). Then click on the serial monitor this is the button with the magnifying glass in the top right-hand corner (circled in yellow). Arduino Analog 2A new window will pop open that should show you an ever changing stream of numbers, this is a value of what your sensor is measuring. If you cover up the light sensor or shine a light on it, the value of the sensor will change depending on what it measures, the more light it gets the higher the reading it produces. Step 3: Now we are going to determine the top and bottom values measured by the sensor. First change delay(1);  to delay(1000); this will slow down the sensor readings to once a second and will make it easier to read the values (do not forget to upload the new code!). Start the serial monitor and get ready to take some readings. Arduino Analog Sensor CoveredFirst cover up the sensor so no light gets to it, do this till the readings stabilize, I got a reading of approximately 220. Then shine a light on the sensor a flashlight or cell phone backlight will do the trick. Again wait for the reading to stabilize and then note the value, mine was approximately 1005. Arduino Analog Sensor Lit Up We have now determined the top and bottom ranges of the sensor. Step 4: Our Arduino is now setup to read an analog light sensor, the next thing we want it to do is to turn an LED on and off depending on the amount of light hitting the sensor. First we need to pick a sensor threshold value that will determine whether the LED should be on or off. I arbitrarily picked a value of 500 this is about 3/8th of the maximum brightness measured and the equivalent of dim lighting. We will setup the code so that if the sensor value is bellow 500 the LED comes on and if it is above 500 the LED will turn off.  To implement this simply copy the code above into your sketch, an explanation of the code is included bellow. Arduino Analog 3 Step 5: Now cover the sensor with your finger, if the LED comes on then you know that it worked! Done!The LED should turn off when you remove your finger. Congratulation you are done! You have just gotten your Arduino to react to an input and you can use this technique with many other sensors and components! Explanation:  Step 1: In this step we wired up the components. The first thing we do is run wires from the Arduino to the breadboards power connections (the lanes on the side of the breadboard with the + and – ) these will power the various components of our project. Use two jumper wires (preferably red (+) and black (-)) connect the red wire from the 5v pin on the Arduino to the + rail and connect the black wire from the GND pin to the – rail on the breadboard. Then we connect the light sensor, like most sensors it has 3 connections (GND, Vcc, and a Signal output). The GND pin on the sensor should be connected using a jumper wire to the – rail on the breadboard. Repeat this with the Vcc pin on the sensor and connect it to the + rail on the breadboard. For analog sensors the signal output pin should be connected via jumper wire to one of the Arduino’s analog inputs (A0 to A5 on an Arduino Uno), in this case we used A0. Now we have fully wired up the sensor so that the Arduino can read it! Analog sensors produce a voltage output that varies depending on the value the sensor is sensing, this is what the Arduino reads through one of the analog pins and converts into a measurement.  Most Analog sensors need a power source as they compare the value they measure (as a voltage) to a known voltage (3.3 or 5v) that the Arduino produces. For an analog sensor to work all three connections must be wired up! Lastly we wired up the LED with a resistor. The LED is being driven by the digital pin 6, pin 6 was chosen as it is a PWM pin which allows us to change the brightness of LED. We also use a resistor to limit the voltage being supplied to the LED, although this is not needed it will save our LED from being overpowered and burning out. Also please note that the Arduino can only supply enough power to run a single LED off of each digital pin. To wire it up connect a jumper wire from pin 6 to one end of the resistor, connect the other end of the resistor to the long leg on the LED (the positive side) , then connect  the short leg of the LED (the negative side) to a pin on the – rail of the breadboard. Step 2:In this step we uploaded some code to the Arduino so that we could read what value the analog sensor was measuring. Without this code the Arduino would not know what we want it to do. Analog sensors connected to the Arduino will produce a value from 0 to 1023 depending on what is being measured and what type of sensor is being used. As the phenomenon being measured changes the value read by the Arduino will also change, in the case of the light sensor as it becomes darker this value will decrease and as it becomes lighter this value will increase. The code in the AnalogReadSerial example is very simple and the minimum code required to read and display an analog sensor value. The included comments do a good job of explaining what each part does but we will look at it in more detail here. Serial.begin(9600);  We include the Serial.begin command in the void setup so that the Arduino knows to communicate back to the computer. The 9600 value refers to the baud rate or how quickly the Arduino communicates with the computer, if you are inexperienced leave this alone. int sensorValue = analogRead(A0); This line in the void loop is the one that reads the value coming from the analog sensor. The int sensorValue portion creates a variable called sensorValue that will store the value read from the sensor. We could have called this anything such as lightsensorValue or sensor1. The analogRead(A0) portion tells the Arduino to read the value of the sensor on pin A0, if we had connected the sensor to another pin then we would need to change the A0 portion to match (A1-A5). Serial.println(sensorValue); This line tells the Arduino to send the reading from the sensor back to the computer so that we can see it on the serial monitor. Note that the sensorValue portion must match what we called the variable in the line above. delay(1);   This line slows down the program by 1 millisecond, this is done to ensure the program stays stable. If you want to slow down the sensor readings coming from the Arduino increase the value of the number in-between the brackets. delay(1000);  would read the sensor once every second. Step 3: Analog sensors connected to the Arduino can only produce a value from 0 to 1023, the value produced is relative to the voltage being output by the sensor.  A value of 0 indicates 0 volts on the analog pin and a value of 1023 signifies 5 volts on the analog pin. The value produced from analog read does not tell us very much on its own about what we are measuring as it does not relate the value produced to what we are trying to measure.  What does a light sensor reading of 500 mean? How many Lux is that? Is that a lot of light or little light? That is why we found the extreme values of the sensor in step 3, this will help us relate the sensor reading to the amount of light being measured. Now we can determine that a reading of 500 is about 3/8th as bright as the light from our cell phone using the following formulas: Sensor Range from light to dark = maximum light sensor reading – minimum light sensor reading = 1005-220 = 785 Adjusted sensor reading = Sensor reading – minimum light sensor reading = 500-220 = 280 Relative amount of light measured = Adjusted sensor reading / Sensor Range from light to dark = 280/785 = 35.6% You can use this to determine at which brightness or darkness you want your LED to turn on! NOTE: This method only works for linear analog sensors and relative measurements. If you must know the precise value of what is being measured by the sensor (say an analog temperature sensor) you will need to find the datasheet for that particular sensor on the internet and add some code. In the datasheet there will be a graph that relates the voltage produced by the sensor to the physical measurement being taken. You need to map these values to each other in order to get the correct reading of the physical phenomenon. We will cover how to do this in another tutorial. Step 4: As you can see we modified the code from the AnlogReadSerial example so that the Arduino turns on an LED when the light sensor reads a value bellow 500. To do this we had to add some simple lines of code which we will explain here. pinMode(6, OUTPUT); This line tells the Arduino that pin 6 should be an output, it is needed to ensure the LED is not turned on accidentally if (sensorValue <= 500) This line is a “test” to see if the program should do something (in this case if the LED should turn on). It states that if the sensorValue is less than or equal to 500 something should be done. What should be done is determined by the next lines of code. By changing the value of 500 we change when the LED comes on, the lower the value the darker it needs to be before the light comes on or vice versa. analogWrite(6, 255); This tells the Arduino to turn on pin 6 to a value of 255. The LED was connected to pin 6 that is why we picked it here, if we plugged the LED into another pin then we would need to change the number to match. The 255 tells the Arduino to turn the LED on to maximum brightness, pin 6 is a PWM pin which means it can change the brightness of the LED. We use analogWrite because this tells the pin to go into PWM mode which allows us to vary the brightness. We could have picked any value from 0 to 255 for this portion, with 0 being off and 255 being maximum brightness. Note that this only works with PWM pins (they have a squiggly line in front of the pin number on the Arduino board). If we pick a non PWM pin then we can only have off or maximum brightness. You can play around with this value to see how it affects your LED. else This line is the second part of the “test” that tells the program to do something, in this case we only go into the else portion of the program if sensorValue is greater than 500 (the reading from the light sensor). analogWrite(6, 0); This line of code is the same as the analogWrite above, except in this case it tells the Arduino to turn off pin 6 which turns off the LED when the else condition is met. We could add more code to the program if we want the LED to react differently to the light sensors values, for example we can make the LED come on at half brightness if it is only a light bright using the following code. Arduino Analog 4 else if (sensorValue <= 800) analogWrite(6,100); Just copy this in-between analogWrite(6, 255); and else. The else if command adds another step to the “test” on how the Arduino should react, you can add as many as you want. In this case it states that the LED should be turned on to half brightness if the light sensor value is above 500 and bellow 800. Another thing we could do is to vary the LED brightness depending on the amount of light falling on the sensor using the following code. In here the LED turns off in high levels of light and varies in brightness bellow the 900 threshold. The darker it is the brighter the LED becomes. Arduino Analog 6 If (sensorValue <= 900){ sensorValue = map(sensorValue,220, 900, 255,0); analogWrite(6, sensorValue); } else analogWrite(6,0); Just replace the entire if and else commands with this code like in the above picture. The only new thing here is the map command. sensorValue = map(sensorValue,220, 900, 255,0); This line of code tells the Arduino to transform the sensorValue into something that can be used by analogWrite. analogwrite can only be used with values from 0 to 255, 0 being off and 255 being maximum brightness. What we have done here is tell the Arduino to change sensorValue from 220 to 255 and from 900 to 0 any sensor values that fall in-between will be linearly mapped to some value in-between.  If you need more information on the map command you can look it up here. Conclusion: While this tutorial is very simple it is a great way to start learning about sensors and how to make the Arduino react to them. For example we could combine this with a relay board in order to turn on a patio light when it gets dark outside or we can use a temperature sensor to turn on a space heater when it gets cold.  With one of our knock sensors and a buzzer you can turn your Arduino into a doorbell! The possibilities are endless and you can get very creative with it. I used a light, temperature, and gas sensor to control the way the lights on my project bellow fade in and out! Thank you for visiting digit makers .

Previous Post Next Post

  • 122
Comments 0
Leave a comment
Your Name:*
Email Address:*
Message: *

Please note: comments must be approved before they are published.

* Required Fields