Photoresistor and Arduino

This guide shows how to operate a photoresistor with Arduino. A photoresistor is a type of resistor whose resistance depends on the light rays falling on it.

The following elements are used in the example:

Connecting a photoresistor from Arduino:

Unlike a phototransistor, the photoresistor has no specific polarity and can be freely connected to the circuit (like an ordinary resistor). The circuit must be connected as follows:

Photoresistor Pin Arduino
First leg A1
Second leg 5 V

Additionally, pin A1 is pulled up to ground by a 1 kΩ resistor, creating a simple voltage divider (the resistor value can be chosen experimentally). The connected circuit is shown in the diagram below:

The diagram of connecting photoresistor with Arduino Uno.

Program for Arduino

The value from the photoresistor is read on the Arduino analog input. After exceeding the set threshold the program lights up the diode connected to pin 13. Additionally, it displays the read value on the serial monitor all the time. The following code was used in the example:

 
int sensor = A1; //pin analogue A1 connected to the photoresistor leg

void setup() {
  Serial.begin(9600); //initialisation of a serial monitor
  Serial.println("---- Photoresistor test ----");
  pinMode(13, OUTPUT); //pin 13 set as output - diode
}

void loop() {
  int war = analogueRead; // reading the value from A1
  Serial.print(war); // display it on a monitor
  if (war > 700) // when the value exceeds a certain threshold, then the LED on pin 13 will light up
  {
    Serial.print(" Diode on");  
    digitalWrite(13, HIGH);
  }
  else
  {
    digitalWrite(13, LOW);
  }
  Serial.println("");
  delay(200); // Delay between readings
}

The effects of the programme can be observed on the following screen:

Serial screenshot of the monitor.

Botland.store - shop for makers!