.. 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 [|link_sf_facebook|] and join today! .. _py_pot: 2.11 Turn the Knob =========================== In the previous projects, we have used the digital input on the Pico. For example, a button can change the pin from low level (off) to high level (on). This is a binary working state. However, Pico can receive another type of input signal: analog input. It can be in any state from fully closed to fully open, and has a range of possible values. The analog input allows the microcontroller to sense the light intensity, sound intensity, temperature, humidity, etc. of the physical world. Usually, a microcontroller needs an additional hardware to implement analog input-the analogue-to-digital converter (ADC). But Pico itself has a built-in ADC for us to use directly. |pin_adc| Pico has three GPIO pins that can use analog input, GP26, GP27, GP28. That is, analog channels 0, 1, and 2. In addition, there is a fourth analog channel, which is connected to the built-in temperature sensor and will not be introduced here. In this project, we try to read the analog value of potentiometer. * :ref:`cpn_pot` **Schematic** |sch_pot| The potentiometer is an analog device and when you turn it in 2 different directions. Connect the middle pin of the potentiometer to the analog pin GP28. The Raspberry Pi Pico contains a multi-channel, 16-bit analog-to-digital converter. This means that it maps the input voltage between 0 and the operating voltage (3.3V) to an integer value between 0 and 65535, so the GP28 value ranges from 0 to 65535. The calculation formula is shown below. (Vp/3.3V) x 65535 = Ap Then program the value of GP28 (potentiometer) as the PWM value of GP15 (LED). This way you will find that by rotating the potentiometer, the brightness of the LED will change at the same time. **Wiring** |wiring_pot| .. #. Connect 3V3 and GND of Pico to the power bus of the breadboard. .. #. Insert the potentiometer into the breadboard, its three pins should be in different rows. .. #. Use jumper wires to connect the pins on both sides of the potentiometer to the positive and negative power bus respectively. .. #. Connect the middle pin of the potentiometer to GP28 with a jumper wire. .. #. Connect the anode of the LED to the GP15 pin through a 220Ω resistor, and connect the cathode to the negative power bus. **Code** .. note:: * Open the ``2.11_turn_the_knob.py`` file under the path of ``euler-kit/micropython`` or copy this code into Thonny, then click "Run Current Script" or simply press F5 to run it. * Don't forget to click on the "MicroPython (Raspberry Pi Pico)" interpreter in the bottom right corner. * For detailed tutorials, please refer to :ref:`open_run_code_py`. .. code-block:: python import machine import utime potentiometer = machine.ADC(28) led = machine.PWM(machine.Pin(15)) led.freq(1000) while True: value=potentiometer.read_u16() print(value) led.duty_u16(value) utime.sleep_ms(200) When the program is running, we can see the analog value currently read by the GP28 pin in the shell. Turn the knob, and the value will change from 0 to 65535. At the same time, the brightness of the LED will increase as the analog value increases. **How it works?** .. code-block:: python potentiometer = machine.ADC(28) Access the ADC associated with a source identified by id. In this example it is GP28. .. code-block:: python potentiometer.read_u16() Take an analog reading and return an integer in the range 0-65535. The return value represents the raw reading taken by the ADC, scaled such that the minimum value is 0 and the maximum value is 65535. * `machine.ADC - MicroPython Docs `_