Experiment 9 from the Inventors Kit for Raspberry Pi Pico, in which we explore a capacitor charging circuit. Included in this resource are code downloads, a description of the experiment and also a video walk-through. This resource provides additional help and is not meant to replace the documentation that ships with the kit.
The Raspberry Pi Pico is a compact microcontroller based on the RP2040 processor - Raspberry Pi’s first in-house designed silicon. The RP2040 on the Pico uses 2 ARM processor cores, and also has 4 programmable IO controllers (PIO). These are like mini processors, and can help the Pico complete more complex tasks than it would otherwise be able to do. The Pico has 28 general purpose IO pins (GPIO), and 3 of these can also be used as Analogue inputs. The Pico can be programmed in several languages. The experiments in these resources are written in the Thonny Editor using MicroPython.
Pico Inventor's Kit Exp' 9 - Capacitor Charge Circuit
In this experiment one of the analog inputs on the Pico will be used to measure the voltage at the positive leg of a capacitor. A circuit will be created that allows the capacitor to be charged through a variable resistor (also known as a potentiometer). The Pico will be programmed to display the charge level of the capacitor using external LEDs to create a colour coded display.
- To charge an electrolytic capacitor through a potentiometer.
- To measure the charge of a capacitor by using an analog input.
- To create a colour coded scale to show capacitor charge level.
Exp 9 - The Code
All experiments are coded in MicroPython in Thonny. To open in Thonny, copy and past the code below directly into the Thonny editor.
''' Kitronik Inventor's Kit Exp 9 Control a set of LEDs to show the charge in a capacitor. ''' import machine import time import math # Setup the pins controlling the LEDs with human-readable names red = machine.Pin(14, machine.Pin.OUT) orange = machine.Pin(15, machine.Pin.OUT) yellow = machine.Pin(16, machine.Pin.OUT) green = machine.Pin(17, machine.Pin.OUT) pot = machine.ADC(26) # Setup the analogue (A0) on GP26 with a human-readable name while True: # First read in the capacitor voltage capVoltage = pot.read_u16() # Do a quick conversion to percentage (65535/655.35 => 100% full scale) capPercent = math.trunc(capVoltage/655.35) # Now we decide which LEDs to turn on if ((capPercent > 25) and (capPercent <= 50)): # Red at 25% and up red.value(1) elif ((capPercent > 50) and (capPercent <= 75)): # Red and Orange at 50% and up red.value(1) orange.value(1) elif ((capPercent > 75) and (capPercent <= 90)): # Red, Orange and Yellow at 75% and up red.value(1) orange.value(1) yellow.value(1) elif (capPercent > 90): # All 4 when we get over 90% red.value(1) orange.value(1) yellow.value(1) green.value(1) else: # Must be below 25%, so turn off all the LEDs red.value(0) orange.value(0) yellow.value(0) green.value(0)
Inventors Kit Extra Resources:
Each of the ten experiments has been designed to ease you into coding and physical computing for the for the Raspberry Pi Pico. The experiments have been chosen to cover the key concepts of physical computing and they also increase in difficulty as you progress. This resource has been put together to provide additional information for this experiment and has not been designed to replace the booklet. The majority of the information you will need to perform and understand this experiment is contained in the booklet.
If you are new to coding and physical computing, even the least complex examples can be quite challenging. With this in mind, we created walk-through videos for each of the experiments. Our presenter talks you through the circuit in a way that backs up the information given in the booklet but in a style that some might find easier to absorb. As well as having a video walk-through, each page also contains the code. Although it is always good to tackle the code yourself, it can be handy for testing your circuit quickly. The code has been heavily commented as an extra learning resource. To get the most out of the experiment, once you've tested your circuit have a go at coding the experiment from scratch.
Follow the links in the table below:
|Exp No.||Experiment Name.|
|Digital Inputs & Outputs.|
|Light Sensor & Analog Inputs.|
|Dimming an LED using a potentiometer.|
|Using a transistor to drive a motor.|
|Control a servo with a potentiometer.|
|Setting the tone with a piezo buzzer.|
|Using a seven segment display.|
|Exploring wind power.|
|Capacitor charge circuit.|
|Controlling ZIP LEDs.|
©Kitronik Ltd – You may print this page & link to it, but must not copy the page or part thereof without Kitronik's prior written consent.