A sensor is a device that measures a physical quantity and converts it into an electric signal. They can be analog (working like potentiometers) or digital (working like switches). Some examples of analog sensors.

Testing a sensor
To see if a sensor works we can read its analog value to control an analog output: an LED in Pulse Width Modulation (PWM) variable brightness.
We send the value to serial as well (open the serial monitor).
You can connect a piezo speaker to have an audio feedback (+pin to ~9 on the Arduino, -pin to ground).

int sensorPin = A0;   //sensor on the Arduino analog 0
int speakerPin = 9;   //speaker on Arduino pin 9
int ledPin = 13;      //led on Arduino pin ~13

//minimum and maximum values detected by the sensor
//it may depend on the sensor and circuit
//you can test them empirically
int minValue = 0;      
int maxValue = 1023;

void setup()
  //setting the digital 
  pinMode(speakerPin, OUTPUT);
void loop()
    int sensorValue = analogRead(sensorPin);    
    //for the LED in PWM (light intensity) the value I read needs to be mapped to 0-255
    int output = map(sensorValue, minValue, maxValue, 0, 255);
    //send it to the pin
    analogWrite(ledPin, output);
    //for the sound we need to map it between 100 and 5000 to be audible
    int value = map(sensorValue, minValue, maxValue, 100, 5000);
    tone(speakerPin, value);
    //print it to the console
    Serial.print("\t Analog input = ");      
    Serial.print("\t Analog output = ");      

Force sensitive resistor

FSRs are sensors that allow you to detect physical pressure, squeezing and weight. More info here.


Flex sensor

This works in a similar way. The code is calibrated for a 2.2 inches Flex sensor and a 10K ohm resistor.
If you have different sensors and resistors just read the value at rest and the value at maximum stress (which probably won’t be 0 and 1024) and map accordingly.


// Sensor pin - GND
// Sensor pin - Analog In 0, with 10K resistor to +5V
int LedPin = 13;    // LED connected to analog pin 13
int SensorPin = A0;    // Sensor connected to analog pin A0

void setup()
    // initialize serial communications

void loop()
    // read the voltage from the voltage divider (sensor plus resistor)
    int sensor = analogRead(SensorPin);

    int output = map(sensor, 730, 900, 0, 255);
    // print out the result
    Serial.print("analog input: ");
    Serial.print(" output: ");
    analogWrite(LedPin, output);      
    // pause before taking the next reading



Photocell are basically resistors that change its resistive value (in ohms Ω) depending on how much light is shining onto the squiggly face.
You can use in a laser tripwire.

Triple Axis Accelerometer

Similar to what you have on the wii or iPhone. Senses acceleration in 3 axis, if calibrated detects the inclination on a plane. More info here.


Analog 2 axis thumb joystick

It’s like the ones on a Playstation or Xbox gamepad. The wiring is straightforward: vcc, ground, X and Y are potentiometers and sel is like a button (Note, it needs a resistor) More info.


Posted in

Post a comment