Chapter 8 Potentiometer & LED
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#define A3 pinbase + 3
#define ledPin 0
int main(void){
int value;
float voltage;
if(wiringPiSetup() == -1){ //when initialize wiring failed,print message to screen
printf("setup wiringPi failed !");
return 1;
}
softPwmCreate(ledPin,0,100);
pcf8591Setup(pinbase,address);
while(1){
value = analogRead(A0); //read A0 pin
softPwmWrite(ledPin,value*100/255);
voltage = (float)value / 255.0 * 3.3; // calculate voltage
printf("ADC value : %d ,\tVoltage : %.2fV\n",value,voltage);
delay(100);
}
return 0;
}
In the code, read ADC value of potentiometers and map it to duty cycle of PWM to control LED brightness.
Python Code 8.1.1 Softlight
First observe the project result, and then analyze the code.
1. Use cd command to enter 08.2.1_Softlight directory of Python code
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/08.1.1_Softlight
2. Use the python command to execute the Python code “Softlight.py”.
python Softlight.py
After the program is executed, shift the potentiometer, then the terminal window will print out the voltage
value of the potentiometer and the converted digital quantity. And brightness of LED will be changed
consequently.
The following is the code:
im port RPi. GPIO as GPIO
im port smbus
im port time
address = 0x48
bus=smbus.SMBus(1)
cmd=0x40
ledPin = 11