Rising and Falling Edge Detection With Raspberry Pi Pico and MicroPython
by clovisf in Circuits > Microcontrollers
580 Views, 0 Favorites, 0 Comments
Rising and Falling Edge Detection With Raspberry Pi Pico and MicroPython
I am showing you how to detect when a button (or any digital input signal, really) goes from low to high or from high to low. It is useful when you quickly need to determine wether a signal changed or not.
Supplies
- Raspberry Pi Pico or Pico W
- Micro USB cable
- One push button NO (normally open)
- One 1/4W 1k Ohm TH resistor
- One 5mm red opaque TH LED
- Breadboard
- Jumper wires
Connect the Raspberry Pi Pico to the Computer
You will need a micro USB cable and a computer with USB ports. Download the Thonny IDE from here. Now we will supply the microPython firmware to the Pico. Press and hold the BOOTSEL button of the Pico while connecting its USB cable to the computer. Once the cable is connected to the computer, release BOOTSEL.
You will see a new drive in your file explorer (e.g. Windows explorer). Download the UF2 file from this source. Drag such file to the new drive that show up in the last paragraph, which is the Pico. It will reboot and you are good to go.
With the Pico connected to the computer, open the Thonny IDE and go to “Tools > Options > Interpreter”. Select “Micropython (Raspberry Pi Pico” and the COM port that represents your Pico (there will ideally be only one, select it). Click “OK”.
There is a console in the bottom of the Thonny IDE, which (if the above steps were successfull) wil show something like this:
MicroPython v1.22.2 on 2024-02-22; Raspberry Pi Pico with RP2040
Type "help()" for more information.
>>> %Run -c $EDITOR_CONTENT
Let’s implement algorithms to falling and rising edge input detector with microPython. They are used to detect when a button or signal went for high to low and vice versa.
As the hardware we will be using a Raspberry Pi Pico programmed with microPython. There will be a single button and LED in hardware side.
Source: https://www.avrfreaks.net/s/topic/a5C3l000000UX2IEAW/t140784
The theory is illustrated in the picture above. We are interested in knowing when the signal goes from high to low (in this case from 3.3V to 0V) or low to high (in this case from 0V to 3.3V).
This code is written in microPython but the idea serves well for any microcontroller, really. Every 10ms I registed the current state of the input.
An if() conditional verifies wether the state of 10ms ago is equal, bigger or smaller (in voltage) than the current one. If it is equal the code does nothing, just feeds the current state in the “old state” variable.
The code below is for the rising edge.
from machine import Pin
import time
onboard = machine.Pin(25, machine.Pin.OUT)
button = Pin(19, Pin.IN, Pin.PULL_DOWN)
oldTime = 0
currentTime = 0
onboard.off()
previousState = False
while True:
currentTime = time.ticks_us()
if(currentTime - oldTime > 10000):
if(button.value()):
if(previousState == False):
onboard.toggle()
previousState = True
else:
pass
else:
pass
previousState = False
And a video of the system working. Notice that the onboard LED light as soon as I press the button.
Now the opposite, I am detecting the falling edge. The code is below.
from machine import Pin
import time
led = machine.Pin(18, machine.Pin.OUT)
button = Pin(19, Pin.IN, Pin.PULL_DOWN)
oldTime = 0
currentTime = 0
led.off()
previousState = False
while True:
currentTime = time.ticks_us()
if(currentTime - oldTime > 10000):
if(not button.value()):
if(previousState == True):
led.toggle()
previousState = False
else:
pass
else:
pass
previousState = True
Notice in the video below that the LED changes state only when I release the button.
Note that if you want to use this code to other cases than toggling an LED, you may insert your code in the place of
led.toggle()
That is where the magic happens.
There are applications where you need either of the techniques, for example in those TV shows that you have to press a button to answer some question. The edge detected is the rising one, which happens as soon as you press the button.