Authorized Online Retailers:

Buy from USA Buy from UK Buy from DE Buy from IT Buy from FR Buy from ES Buy from JP

In this lesson, you will learn how to read Analog data from an ADC pin and use PWM to output Analog signal to external device.

ADC is abbreviation of Analog to Digital Convert.   Digital signal has only two status, 1/0 or High/Low voltage. Analog signal  is any continuous signal for which the time-varying feature of the signal is a representation of some other time-varying quantity.
In this lesson, we will use a photoresistance as analog data source to ADC2(GP28) pin.

PWM abbreviation of Pulse-width modulation. PWM pin can let Pico to export analog current to actuators such as LED, servo, DC motors.
In this lesson, we will use an LED as analog device and output PWM signal to this device through GP15 pin.

You can the location of GP15 and GP28 pin in following picture 1.1.

In the project, we will imitate an automatic light system: when sensor detects that natural environment light is dark, we will give more power to man-made light( LED ) , when natural environment is bright, then we reduce the light from LED.

Picture 1.1

  1. Raspberry Pi Pico board and microUSB cable
  2. A computer to run Thonny Python IDE
  3. A breadboard
  4. LED x 1 pc
  5. 220 Ω resistor x 1 pc
  6. 10 kΩ  resistor x 1 pc
  7. photoresistance x 1 pc
  8. Some jumper wires

In above circuit graph, you can see that:
LED negative pin connected to GND
LED Positive pin connected to GP15 through 220 Ω resistor
photoresistance pin 1 connected to 3.3V through 10 kΩ resistor
photoresistance pin 2 connected to GP28

You can download lesson 3 .python code from http://osoyoo.com/driver/pico/lesson3/pico-lesson3.py

You can use Thonny to open pico-lesson4.py and load it to Pico later.
Here is the full code with comments:

from machine import Pin, PWM, ADC #import Pin, PWM, ADC handling library

pwm = PWM(Pin(15)) #tell Pico GP15 is a PWM output   pin
adc = ADC(Pin(28)) #tell Pico GP28 is a ADC analog input pin

pwm.freq(1000) #set PWM frequency at 1000 hz

while True:
	duty = adc.read_u16() #read  Light intensity
	pwm.duty_u16(60000-duty) #provide current to LED, the more light intensity, the less current to LED

Step 1: Connect Pico board to one of USB ports in your PC
Step 2: If you haven’t installed Thonny Software or don’t know how to use Thonny IDE, please read lesson 1

Step 3: Now open the Thonny Python IDE, click Run to select MicroPython for Rapsberry Pi Pico as Interpreter :

Also please select the COM port which your Pico board is connected :

After that, click OK to save the setting.

Step 4: Now copy the python code to Thonny window as following:

Step 5: Click the little ► button  ,the save page will pop up as following photo ;

select Raspberry Pi Pico as destination.

Step 7:Name the file as pico-lesson3.py, then click OK

Step 8: Click the little ► button again to run the Python code.

Now you can try to press put a bright light over the photoresistance, you will see that the LED will become very dark. If you use your hand to shade light over photoresistance , then you will the LED will become bright.

Learn More about parts SKU:2021005900

No. Picture Product Name Link
1 Raspberry Pi Pico Board https://osoyoo.store/products/raspberry-pi-pico-flexible-microcontroller-board-based-on-the-raspberry-pi-rp2040-dual-core-arm-cortex-m0-processor-1-pc?variant=39875307864175
2 Servo motor https://osoyoo.store/products/micro-servo-sg90-blue-for-arduino-v2-0-robot-carmodel-lacc200610?variant=31648847560815
3 Infrared Sensor Module https://osoyoo.store/products/hc-sr501-pir-motion-sensor-for-arduino?variant=31957812838511
4 RFID Module and card https://osoyoo.store/products/osoyoo-rfid-module?variant=31970646130799
5 Push Buttons and Hats https://osoyoo.store/products/push-button-and-hats-5pcs?variant=39879253131375
6 LED(6 x White, 6 x Red, 6 x Yellow, 6x Green) https://osoyoo.store/products/led-packs-for-arduino?variant=31957816442991
7 Servo motor https://osoyoo.store/products/sg90-micro-servo-for-arduino-raspberry-pi-robot?variant=31957821096047
8 Piezo Buzzer Module https://osoyoo.store/products/piezo-buzzer-module-1pcs?variant=39879251656815
9 I2C LCD Display(16×2) https://osoyoo.store/products/i2c-lcd1602-display?variant=39875437101167
10 Philips Screwdriver https://osoyoo.store/products/phillips-screwdriver?variant=31930635780207https://osoyoo.store/products/single-channel-relay-module-for-arduino-raspberry-pi?variant=31955954401391
11 Solderless Prototype Breadboard https://osoyoo.store/products/solderless-prototype-breadboard-1pcs?variant=39879255457903
12 Potentionmeter (10K adjustable resistor) https://osoyoo.store/products/potentionmeter-10k-adjustable-resistor-3pcs?variant=39879281016943
13 40Pin M to M Jumper Wires https://osoyoo.store/products/40pin-m-to-m-jumper-wires?variant=39879256539247
14 20Pin M to F Jumper wires 15cm https://osoyoo.store/products/20pin-15cm-female-to-female-cable?variant=31930663305327