EFA:Sensors:Reading Analog Sensors

Variable Resistors

Many sensors are just variable resistors that change their resistance value based on some external input (light, temperature, force etc.). The Arduino is not able to read the change in resistance directly, but we can convert that resistance change into a change in voltage using a voltage divider.

Voltage Divider

If you connect two resistors in series as in the image below, the voltage read from Vout depends on the ratio of the two resistors. Read the Wikipedia article or check this tutorial from Sparkfun, if you want to learn how to calculate the values.

Light Dependant Resistor (LDR, photocell, light sensor)

This can be used to read the values from various sensors, such as the light sensor we are using.

screen-shot-2016-10-20-at-14-08-58

screen-shot-2016-10-20-at-14-12-40

// Read and display the value from the light sensor.

int lightSensor = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  lightSensor = analogRead(A0);
  Serial.print("light: ");
  Serial.println(lightSensor);
}

Potentiometer

Potentiometers are essentially voltage dividers built inside a handy interface. They come in different shapes and sizes, but all of them work roughly the same way.

Most potentiometers have three pins, I’m calling them A, W and B

  • A would be connected to the negative side of your power supply. GND on the Arduino Uno.
  • W is the pin that outputs a varying voltage (essentially, it’s the Vout on the voltage divider circuit above). The W is connected to a moving wiper that changes the balance of the “two resistors” inside the potentiometer.
  • B would be connected to the positive side of your power supply. 5V on the Arduino Uno.

Sensors with Analog Voltage Output

Many sensors will just give you an analog voltage. These are getting more rare these days as more and more sensors are moving to being completely digital.