You are on page 1of 5

Analog Inputs – Sensors

The ease with which an Arduino can obtain sensor values is one of the features that makes it
so useful.

Digital inputs will give us “1” or “0”. “1” is 5 volts and “0” is 0 volts.  This is useful for a
switch, but for most sensors we need analogic inputs with lots of grades between 0 and 5
volts.

When the sensor gives us a value, the Arduino's analog-to-digital converter (ADC) then
converts that value to a value between 0 and 1023 (1023 would mean 5 volts)

 There are many different types of sensors. Some examples are:

 Light  sensor
 Motion  sensor
 Temperature  sensor
 Humidity  sensor
 Pressure  sensor
 Sound sensor
 Position sensor

These sensors are used in thousands of different applications, including manufacturing,


machinery, aerospace, automobiles, medicine, and robotics.

Light Sensor

Let’s start using the light sensor. It is usually called photocell or photoresistor, and it looks
like this:

Look at the way you have to connect it to the board:

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 1
You will remember the push button with the “pull down resistor”. It is connected in a
similar way. Use A 10K Ohms as a resistor.

In the picture the sensor is connected to the A0 input.

 How does it work?  It is a resistor which changes depending on the light it receives.
The more light, the smaller the resistor, so the value we get is higher. (the value is
always from 0 to 1023)

Now write the sketch in order to send the values to the serial monitor. It should be
something like this:

int photocellPin = 0; // the cell and 10K pulldown are connected to A0


int photocellReading; // the analog reading from the sensor divider

void setup()

Serial.begin(9600); 

// We'll send debugging information via the Serial monitor.


}
void loop()

{
    photocellReading = analogRead(photocellPin);
    Serial.print("Analog reading = ");
    Serial.println(photocellReading); // the raw analog reading
    delay(1000); //it gives us a value every second        
}

Try it and see the different value you get if the sensor is covered, and so on.

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 2
EXERCISE 1
After looking at the values you get from the sensor when you cover it with your hand or not,
connect a LED to Arduino and write the code to make it turn on and off automatically
depending on the light we have. This is similar to the code you would need to connect the
light automatically at home when it is dark and disconnect it when there is enough light.

You need to understand the values, and the “IF” command we have studied, combined with
“<” or “>” .

Save the sketch as “sensors exercise 1”

TEMPERATURE SENSOR

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 3
You can see that in this case we are connecting the sensor without resistor. Be careful with
the polarity and nothing else.

Now we need to convert the value we read in pin A0 (from 0 to 1023) into Celsius. We
will do it in two steps:
1.- If you're using the 5 volts pin and connecting the sensor directly into an Analog pin, you
can use these formulas to turn the 10-bit analog reading into a temperature:

Voltage at pin in milliVolts = (reading from A0) * (5000/1023)


This formula converts the number 0-1023 from the ADC into 0-5000mV (= 5V)

If you're using the 3.3V pin, you'll want to use this:


Voltage at pin in milliVolts = (reading from A0) * (3300/1024)
This formula converts the number 0-1023 from the ADC into 0-3300mV (= 3.3V)

2.- Then, to convert millivolts into temperature, use this formula:


Centigrade temperature = (analog voltage in mV) / 10
(It depends on the model and factory, but the LM35 is supposed to give us 10 mv. for each
Celsius grade, so we would get 200 mv. when we have 20º C)

Once we understand these steps, we can do it directly with a variable which calculates this:
Celsius = (reading from pin A0) * 0’488
Another thing is important. With these values, we should use a float variable.
The difference between float and int is that float is much more exact because it accepts
decimals.
The Code to see the temperature in the Serial Monitor would be the following:

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 4
float AN5;
void setup()
{
Serial.begin(9600);
}
void loop()
{
AN5=analogRead(A5); //We read the digital value in A5 (from 0 to 1023)
AN5= 0.488*AN5; // This converts the digital value to Celsius. It uses ‘.’ not ‘,’
// In the previous line you see we use the same variable AN5 for the calculation
Serial.println(AN5); // We send the data to the monitor.
delay(1000); // We wait for a second to read the value again.   
}

Try it and see the differences in the values you get when you touch the sensor with your fingers.

EXERCISE 2
When the temperature is higher than 30º, you will connect a green LED (this could be the fan -
“ventilador”) and when it is lower than 25º, we will connect the red LED (this is our heater).
Between 25º and 30º, nothing is connected because it is not necessary.
Create two variables for this exercise:
int MAX= 30;
int MIN= 25;
And use an IF command to connect the pins if the condition is true. Do not forget to open the serial
monitor to control the way it works.
Save it as “Sensors exercise 2”

EXERCISE 3
The program will ask us the temperature we want for the MAX and MIN variables.
Each time we reset the Arduino board, it will wait for us to give the values. We will write the values
using the Serial communication we explained in “SERIAL COMMUNICATION 2”
Save it as “Sensors exercise 3”
 

Doctor Areilza, 32. 48010 Bilbao Tel. 944 271 818 Fax 944 396 098 E-mail: colegio@indautxujesuitak.org 5

You might also like