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’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 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) * 65535

Pico’s ADC Pins

pin_adc

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.

What You’ll Need

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

Newton Lab Kit

450+

Newton Lab Kit

You can also buy them separately from the links below.

SN

COMPONENT

QUANTITY

LINK

1

Raspberry Pi Pico 2

1

BUY

2

Micro USB Cable

1

3

Breadboard

1

BUY

4

Jumper Wires

Several

BUY

5

Resistor

1(220Ω)

BUY

6

LED

1

BUY

7

Potentiometer

1

BUY

Circuit Diagram

sch_pot

Wiring Diagram

wiring_pot

Writing the Code

Note

  • Open the 2.11_turn_the_knob.py file under the path newton-lab-kit/micropython or copy the code below into Thonny. Then click “Run Current Script” or press F5 to run it.

  • Ensure that the “MicroPython (Raspberry Pi Pico).COMxx” interpreter is selected in the bottom right corner of Thonny.

  • For detailed instructions, refer to Open and Run Code Directly.

import machine
import utime

# Initialize ADC on GP28
potentiometer = machine.ADC(28)

# Initialize PWM on GP15
led = machine.PWM(machine.Pin(15))
led.freq(1000)  # Set PWM frequency to 1000Hz

while True:
    # Read the analog value (0-65535)
    value = potentiometer.read_u16()
    print("Potentiometer value:", value)

    # Set the LED brightness
    led.duty_u16(value)

    # Small delay to stabilize readings
    utime.sleep_ms(200)

When you run the program, the LED’s brightness will change as you turn the potentiometer knob. Additionally, the console will display the current analog value read from the potentiometer.

Understanding the Code

  1. Analog Reading:

    • potentiometer = machine.ADC(28) initializes the ADC on pin GP28.

    • value = potentiometer.read_u16() reads the analog voltage from the potentiometer and returns a 16-bit integer between 0 and 65535.

      • 0 corresponds to 0V.

      • 65535 corresponds to 3.3V (the Pico’s operating voltage).

  2. Controlling the LED with PWM:

    • led = machine.PWM(machine.Pin(15)) sets up PWM on pin GP15.

    • led.freq(1000) sets the PWM frequency to 1000Hz.

    • led.duty_u16(value) sets the duty cycle of the PWM signal based on the potentiometer’s reading.

      • A higher value increases the duty cycle, making the LED brighter.

      • A lower value decreases the duty cycle, dimming the LED.

  3. Printing the Value:

    • print("Potentiometer value:", value) outputs the current analog value to the console for monitoring.

Experimenting Further

  • Change the PWM Frequency: Try different frequencies with led.freq() and observe the effect on the LED.

  • Map the ADC Value: Introduce a scaling factor or map the ADC value to a different range to see how it affects LED brightness.

  • Use Other ADC Pins: Connect the potentiometer to GP26 or GP27 and adjust the code accordingly.

Troubleshooting Tips

  • LED Not Changing Brightness:

    • Ensure the LED and resistor are connected correctly.

    • Verify that the potentiometer is wired properly.

  • Incorrect ADC Values:

    • Check the connections to GP28.

    • Make sure the potentiometer’s outer pins are connected to 3.3V and GND.

Conclusion

By integrating analog input with PWM output, we’ve created a simple yet powerful way to control the brightness of an LED using a potentiometer. This project demonstrates how to read analog signals and use them to control other components, a fundamental skill in electronics and microcontroller programming.

References