ARDUINO 2: Sensors

INPUTS AND OUTPUTS
Revision – functions from last week:

DIGITAL SENSORS

Microcontrollers are powerful ways to connect inputs and outputs. Incoming voltage can be read from sensors and interpreted as data. And just like with when we use actuators (outputs), sensors can also be digital or analog. A button, or a switch which is on or off, are both examples of a digital sensors because they only two states. A volume knob, also known as a potentiometer, is an example of a analog sensor. It produces a range of values. In your kits you have a light sensor and a temperature sensor both of which are analog sensors.

Digital sensors can be controlled on the digital pins (1-13), simply be setting them to an input pin. This means they are listening for voltage changes instead of sending them out. We do this in code as follows:

int inPin = 7;   // pushbutton connected to digital pin 7 
 
void setup(){
  pinMode(inPin, INPUT);      // sets the digital pin 7 as input
}
void loop(){
  val = digitalRead(inPin);   // read the input pin
}

This sets pin 7 as inPin. And then sets it as an INPUT pin (meaning it will take a sensor). We then read from the pin by using the function digitalRead();

02A CONNECTING A BUTTON

See this guide and make a circuit that connects a digital sensor, a button to an LED. http://arduino.cc/en/tutorial/button

This example uses a pull down resistor. Buttons often require what are called pull down or pull up resistors. This is a way of controlling their voltage when their circuit is open. Open circuits can have a voltage that floats around making it difficult for the Arduino to read. So we put a resistor in to make sure the voltage doesn’t float when the button is open.

02B ANALOG SENSORS

Analog sensors must be connected to analog pins. (A0-A5). They are set using very similar code except they use the function analogRead(); instead of digitalRead();

By using the analogRead() function, we can read the voltage applied to one of the pins. This function returns a number between 0 and 1023, which represents voltages between 0 and 5 volts. For example, if there is a voltage of 2.5 V applied to pin number 0, analogRead(0) returns 512.

Set up you board with the potentiometer as follows:

potenitometer

 

 

//Example 02B - Potentiometer
int analogPin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3. Outside leads to ground and +5V 
 
int val = 0; // variable to store the value read
 
void setup(){ 
  Serial.begin(9600); // setup serial
 } 
void loop(){ 
  val = analogRead(analogPin); // read the input piN
  Serial.println(val); // debug value 
}

Notice we don’t have to set the pin in void setup to INPUT. This is because A0-A5 are always input pins.

Open up you serial monitor and have a look at the values coming from your potentiometer.

 

02C LIGHT SENSOR EXAMPLE

If you now build the circuit below, using560 Ohm Resistor (Green-Blue-Brown) and run the code listed in Example 02B, you’ll see the onboard LED

(you could also insert your own LED into pins 13 and GND as shown in “Blinking an LED” in Chapter 4) blinking at a rate that’s dependent upon the amount of light that hits the sensor.

LDR1-1-620x436

 

// Example 02C: Blink LED at a rate specified by the
// value of the analogue input
 
int LED = 13; // the pin for the LED
int val = 0;   // variable used to store the value
               // coming from the sensor
void setup() {
  pinMode(LED, OUTPUT); // LED is as an OUTPUT
  // Note: Analogue pins are
  // automatically set as inputs
}
void loop() {
  val = analogRead(0); // read the value from
                       // the sensor
  digitalWrite(13, HIGH); // turn the LED on
  delay(val); // stop the program for
              // some time
  digitalWrite(13, LOW); // turn the LED off
  delay(val); // stop the program for
              // some time
}

02D – REACTIVE LIGHT

Now, try Example 02D: but before you do, you’ll need to modify your circuit. Take a look at the next figure again and hook the LED up to pin 9 as shown. Because you’ve already got some stuff on the breadboard, you’ll need to find a spot on the breadboard where the LED, wires, and resistor won’t overlap with the LDR circuit.

 

// Example 02D: Set the brightness of LED to
// a brightness specified by the
// value of the analogue input
int LED =9;  // the pin for the LED
int val = 0;   // variable used to store the value
               // coming from the sensor
void setup() {
  pinMode(LED, OUTPUT); // LED is as an OUTPUT
  // Note: Analogue pins are
  // automatically set as inputs
}
void loop() {
  val = analogRead(0); // read the value from
                       // the sensor
  analogWrite(LED, val/4); // turn the LED on at
                           // the brightness set
                           // by the sensor
  delay(10); // stop the program for
             // some time
}

NoTe: we specify the brightness by dividing val by 4, because analogRead() returns a number up to 1023, and analogWrite() accepts a maximum of 255.

Try Other Analogue Sensors
Using the same circuit that you have seen in the previous section, you can connect a lot of other resistive sensors that work in more or less the same way. For examples, see the booklet included in your kits and try the force sensor example on page 32.