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!
2.2.1 Photoresistor(MCP3008)ο
Note
Depending on your kit version, please identify whether you have ADC0834 or MCP3008 and proceed with the matching section.
Introductionο
Photoresistor is a commonly used component of ambient light intensity in life. It helps the controller to recognize day and night and realize light control functions such as night lamp. This project is very similar to potentiometer, and you might think it changing the voltage to sensing light.
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 |
|---|---|---|
Raphael Kit |
337 |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
|---|---|
- |
|
Schematic Diagramο
T-Board Name |
physical |
WiringPi |
BCM |
|---|---|---|---|
SPICE0 |
pin24 |
10 |
8 |
SPIMOSI |
pin19 |
12 |
10 |
SPIMISO |
pin21 |
13 |
9 |
SPISCLK |
pin23 |
14 |
11 |
GPIO22 |
pin15 |
3 |
22 |
Experimental Proceduresο
Step 1: Build the circuit.
Step 2: Go to the folder of the code.
cd ~/raphael-kit/c/2.2.1-2/
Step 3: Compile the code.
gcc 2.2.1_Photoresistor.c -o photoresistor -lwiringPi -lm
Step 4: Run the executable file.
./photoresistor
When the code is running, the brightness of the LED will change according to the light intensity sensed by the photoresistor.
Note
If it does not work after running, or there is an error prompt: "wiringPi.h: No such file or directory", please refer to Install and Check the WiringPi.
Code
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include <stdio.h>
#include <softPwm.h>
#define SPI_CHANNEL 0 // Use SPI channel 0 (CE0)
#define SPI_SPEED 1000000 // 1 MHz SPI speed
#define LedPin 3 // GPIO3 (WiringPi) for LED PWM
// Read ADC value from MCP3008, channel 0~7
int readMCP3008(int channel) {
if (channel < 0 || channel > 7) return -1;
unsigned char buffer[3];
buffer[0] = 1; // Start bit
buffer[1] = (8 + channel) << 4; // SGL/DIF = 1, D2-D0 = channel
buffer[2] = 0;
wiringPiSPIDataRW(SPI_CHANNEL, buffer, 3);
// Combine the result
int result = ((buffer[1] & 3) << 8) | buffer[2];
return result;
}
int main(void) {
if (wiringPiSetup() == -1) {
printf("wiringPi init failed!\n");
return 1;
}
if (wiringPiSPISetup(SPI_CHANNEL, SPI_SPEED) == -1) {
printf("SPI setup failed!\n");
return 1;
}
softPwmCreate(LedPin, 0, 100); // Init software PWM
while (1) {
int analogVal = readMCP3008(0); // Read from CH0
printf("ADC Value: %d\n", analogVal);
// Scale 10-bit ADC value (0β1023) to PWM range (0β100)
int pwmVal = analogVal * 100 / 1023;
softPwmWrite(LedPin, pwmVal);
delay(100);
}
return 0;
}
Code Explanation
The codes here are the same as that in 2.1.7 Potentiometer. If you have any other questions, please check the code explanation of 2.1.7 Potentiometer(MCP3008) for details.