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.11 Turn the Knob
In this lesson, we’ll explore how to read analog input using the Raspberry Pi Pico 2 W’s built-in Analog-to-Digital Converter (ADC) and use that input to control the brightness of an LED. Specifically, we’ll use a potentiometer—a variable resistor—as an analog input device. By turning the knob of the potentiometer, we’ll adjust the voltage level read by the Pico, which we’ll then use to control the LED’s brightness via Pulse Width Modulation (PWM).
Understanding Analog Input
So far, we’ve worked with digital inputs and outputs, which are either ON (high voltage) or OFF (low voltage). However, many real-world signals are analog, meaning they can vary continuously over a range of values. Examples include light intensity, temperature, and sound levels.
The Raspberry Pi Pico 2 W has a built-in ADC that allows it to read analog voltages and convert them into digital values that can be processed in code.
The ADC converts the analog voltage from the potentiometer into a digital value using the formula:
Digital Value = (Analog Voltage/3.3V) * 1023
Pico’s ADC Pins
The Pico has three GPIO pins that can be used for analog input:
GP26 (ADC0)
GP27 (ADC1)
GP28 (ADC2)
In addition, there’s a fourth ADC channel connected internally to a temperature sensor (ADC4), which we’ll explore in later lessons.
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 |
PURCHASE LINK |
---|---|---|
Pico 2 W Starter Kit |
450+ |
You can also buy them separately from the links below.
SN |
COMPONENT INTRODUCTION |
QUANTITY |
PURCHASE LINK |
---|---|---|---|
1 |
1 |
||
2 |
Micro USB Cable |
1 |
|
3 |
1 |
||
4 |
Several |
||
5 |
1(220Ω) |
||
6 |
1 |
||
7 |
1 |
Schematic
Wiring
Code
Note
You can open the file
2.11_turn_the_knob.ino
under the path ofpico-2w-kit-main/arduino/2.11_turn_the_knob
.Or copy this code into Arduino IDE.
Don’t forget to select the board(Raspberry Pi Pico) and the correct port before clicking the Upload button.
// Define the pins
const int potPin = 28; // Potentiometer connected to GP28 (ADC2)
const int ledPin = 15; // LED connected to GP15 (PWM capable)
void setup() {
// Initialize serial communication for debugging
Serial.begin(115200);
// Set up the LED pin as output
pinMode(ledPin, OUTPUT);
}
void loop() {
// Read the analog value from the potentiometer (0-1023)
int sensorValue = analogRead(potPin);
// Print the sensor value for debugging
Serial.println(sensorValue);
// Map the sensor value to a PWM value (0-255)
int brightness = map(sensorValue, 0, 1023, 0, 255);
// Set the brightness of the LED
analogWrite(ledPin, brightness);
// Small delay for stability
delay(10);
}
When the code is running and the Serial Monitor is open:
As you turn the potentiometer knob, the brightness of the LED should change smoothly from dim to bright.
You should see the analog values printed, ranging from approximately 0 to 1023 as you adjust the potentiometer.
Understanding the Code
Defining the Pins:
Assigns the GPIO pins used for the potentiometer and the LED.
const int potPin = 28; // Potentiometer connected to GP28 (ADC2) const int ledPin = 15; // LED connected to GP15 (PWM capable)
Initializing Serial Communication:
Starts serial communication, allowing you to print messages to the Serial Monitor.
Serial.begin(115200);
Reading the Analog Value:
Reads the analog voltage on potPin (GP28) and returns a value between 0 and 1023.
int sensorValue = analogRead(potPin);
Printing the Sensor Value:
Prints the current sensor value to the Serial Monitor for debugging purposes.
Serial.println(sensorValue);
Mapping the Sensor Value:
Converts the sensor value (0-1023) to a brightness value suitable for PWM output (0-255).
int brightness = map(sensorValue, 0, 1023, 0, 255);
Setting the LED Brightness:
Adjusts the brightness of the LED by setting the PWM duty cycle on ledPin (GP15).
analogWrite(ledPin, brightness);
Adding a Small Delay:
A short delay to stabilize the readings and prevent the loop from running too fast.
delay(10);
Further Exploration
Display Voltage: Modify the code to calculate and display the actual voltage read from the potentiometer.
// Define the pins const int potPin = 28; // Potentiometer connected to GP28 (ADC2) const int ledPin = 15; // LED connected to GP15 (PWM capable) void setup() { // Initialize serial communication for debugging Serial.begin(115200); // Set up the LED pin as output pinMode(ledPin, OUTPUT); } void loop() { // Read the analog value from the potentiometer (0-1023) int sensorValue = analogRead(potPin); // Print the sensor value for debugging Serial.println(sensorValue); // Calculate and display the actual voltage float voltage = sensorValue * (3.3 / 1023.0); Serial.print("Voltage: "); Serial.print(voltage); Serial.println(" V"); // Map the sensor value to a PWM value (0-255) int brightness = map(sensorValue, 0, 1023, 0, 255); // Set the brightness of the LED analogWrite(ledPin, brightness); // Small delay for stability delay(10); }
Control Multiple LEDs: Use multiple potentiometers to control different LEDs or colors in an RGB LED.
Use with Other Sensors: Replace the potentiometer with another analog sensor, such as a light-dependent resistor (LDR), to control the LED based on ambient light.
Explanation of Concepts
Analog-to-Digital Conversion (ADC):
The ADC on the Pico converts the analog voltage from the potentiometer into a digital value.
The voltage range from 0V to 3.3V is converted into a numerical value between 0 and 1023.
Pulse Width Modulation (PWM):
PWM is a technique used to simulate an analog voltage by rapidly switching a digital pin between HIGH and LOW states.
By adjusting the proportion of time the signal is HIGH (duty cycle), we can control devices like LEDs and motors.
Mapping Values:
The
map()
function scales one range of values to another.In this case, we map the potentiometer’s 0-1023 range to the PWM’s 0-255 range.
Conclusion
In this lesson, you’ve learned how to read analog input from a potentiometer using the Raspberry Pi Pico’s ADC and use that input to control the brightness of an LED via PWM. This fundamental skill allows you to interface with a variety of analog sensors and control outputs in a proportional manner.