Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
Photoresistor
Overview
In this lesson, you will learn about Photoresistor. Photoresistor is applied in many electronic goods, such as the camera meter, clock radio, alarm device (as beam detector), small night lights, outdoor clock, solar street lamps and etc. Photoresistor is placed in a street lamp to control when the light is turned on. Ambient light falling on the photoresistor causes street lamps to turn on or off.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
|---|---|---|
Elite Explorer Kit |
300+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
|---|---|
- |
|
Wiring
In this example, we use analog pin ( A0 ) to read the value of photoresistor. One pin of photoresistor is connected to 5V, the other is wired up to A0. Besides, a 10kΩ resistor is needed before the other pin is connected to GND.
Schematic Diagram
Code
Note
You can open the file
01-photoresistorunder the path ofelite-explorer-kit-main\basic_project\01-photoresistordirectly.Or copy this code into Arduino IDE.
1/*
2 The code reads the analog value from a photoresistor connected to pin A0 and sends the
3 value to the serial monitor. The baud rate for serial communication is set to 9600.
4 The code then waits for 10 millisecond before repeating the process.
5
6 Board: Arduino Uno R4
7 Component: Photoresistor
8*/
9
10
11// Define the pin where the photoresistor is connected
12const int photoresistorPin = A0;
13
14// Initialize the serial communication at 9600 baud rate
15void setup() {
16 Serial.begin(9600);
17}
18
19// Main loop where the program runs
20void loop() {
21 // Read the analog value from the photoresistor
22 int sensorValue = analogRead(photoresistorPin);
23
24 // Send the sensor value to the serial monitor
25 Serial.println(sensorValue);
26
27 // Wait for 10 millisecond before the next loop iteration
28 delay(10);
29}
After uploading the codes to the uno board, you can open the serial monitor to see the read value of the pin. When the ambient light becomes stronger, the reading will increase correspondingly, and the pin reading range is 「0」~「1023」. However, according to the environmental conditions and the characteristics of the photoresistor, the actual reading range may be smaller than the theoretical range.