LDR/Photo-resister with Arduino: This articles aims to show the reader how to use an LDR, also known as a photo-resistor in their projects.
How to use an LDR with Arduino
After reading this article, the reader will be able to connect and read a photo-resistor, and use the code in their future projects!
Components Needed
- Arduino
- Breadboard
- Jumper Wires (Male to Male)
- Photoresistor
- 1x 220 Ohm resistor
Wire all the components as per the circuit diagram given below.
How do we measure resistance
The LDR varies its resistance based on light hitting it, and the Arduino technically only reads voltages, so how do we measure resistance? We are not REALLY measuring the resistance in this circuit, instead we are creating a resistor divider network between the LDR and the resistor, which will vary the voltage directly based on the LDRs value. We then read the voltage out of the divider network and print that value.
Code
//Variables int inPin = A0; //Pin the sensor is connected to int sensorVal = 0; //Variable to store sensor data void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println("Serial Communication started...\n"); } void loop() { // put your main code here, to run repeatedly: sensorVal = analogRead(inPin); //analogRead will read the voltage on the pin specified and return it as a value between 0 and 1024. Serial.println(sensorVal); //Print the sensor reading to the serial window so we can view the data. }
Comments
Post a Comment