Categories
Hybrid Pedal My Designs Raspberry Pi Pico

Raspberry Pi PICO – Controlling Relays

This time around playing with Raspberry Pi Pico I’ll cover controlling relays. This is not so much continuation of my PICO MIDI project, but rather continuation of my Hybrid Pedal project that I had started a long time ago 🙂. I covered a lot of things around Pico so have a look at the above links, you might find it useful.

Let’s cover relays. Keep on reading or, as usual – you can skip directly to the video and come back later for more details if you wish so.

Here’s the ToC:

Intro

Here, I’m continuing from my previous post (basics of using relays). In it, I used something similar to the electrical switching used in many traditional pedals (read Boss, Ibanez …) to control relays. Now, let’s add some brains to it 🙂.

First off, to emphasize the point: relays used here are signal level relays – I’m not qualified to give any advice on high voltage relays, so we keep using up to 9V power supply.

With that out of the way, in my previous post I covered how to control monostable and bistable relays. The power supply I used there was 9V, but, if you followed any of my previous PICO related posts, PICO uses 3.3V for operation. Luckily, I used 3V relays (hey, I thought through this well in advance 😉).

As you will see, it is … and it isn’t that straight forward to convert the schematics. In theory, I should just replace the control circuit. That’s how I planned my previous post and it should be that simple. Let’s see if that’s how it pans out.

Schematic

Here’s the updated schematic. All I did was to replace the control circuit and wire up the relay circuitry to 3.3V supply:

Schematic showing how to control a signal relay using Raspberry Pi PICO
Controlling Monostable Relay with Pico (click for full image)

Let’s ignore bistable one for the moment. If you look real hard, you can see that the only difference in the monostable circuit is that the power supply is 3.3V, and please note that R3 had to be changed to 10 ohms. Before, I used 9V supply so the resistor had to have bigger value in order not to damage the relay.

You can see that the power supply comes from Pico’s internal supply. Pico’s regulator can supply about 200mA so we should be good despite monostable relay drawing current constantly while kept turned on. If you look at my post on how to power Pico from external supply, we could use 9V here and then the circuit would be pretty much the same as in the previous post. But in case we’re using USB power supply, as I did in the demos, the above is how we wire it up.

One important note – we can’t just use Pico’s GPIO output and control relay directly, we need to use transistor (Q1) there because GPIO can’t supply enough current and will get damaged.

Software – Monostable

Circuit around Pico is super simple, just a button and one control GPIO. I covered how to use buttons with Pico before. So let’s use that knowledge:

from machine import Pin
import uasyncio as asyncio
from primitives import Pushbutton

def toggle(led, control):
    led.toggle()
    control.toggle()

async def my_app():
    pin = Pin(14, Pin.IN, Pin.PULL_UP)
    red = Pin(15, Pin.OUT)
    control = Pin(18, Pin.OUT)
    pb = Pushbutton(pin)
    pb.press_func(toggle, (red, control,))
    while True:
        await asyncio.sleep(60)

asyncio.run(my_app())  # Run main application code

I kept LED there (even though it isn’t shown on the schematic) so we see something happening on the demo. This is very simple though, if button is pressed, I just toggle control GPIO value. That’s it for “latching” version of control.

If I wanted “momentary” version of control, I do the same thing, just instead of “latching” I need to handle both “press_func” and “release_func”. Simples:

from machine import Pin
import uasyncio as asyncio
from primitives import Pushbutton

def toggle(led, control):
    led.toggle()
    control.toggle()
    
def control_on(led, control):
    led.on()
    control.on()
    
def control_off(led, control):
    led.off()
    control.off()

async def my_app():
    pin = Pin(14, Pin.IN, Pin.PULL_UP)
    red = Pin(15, Pin.OUT)
    control = Pin(18, Pin.OUT)
    pb = Pushbutton(pin)
    #pb.press_func(toggle, (red, control,))
    pb.press_func(control_on, (red, control,))
    pb.release_func(control_off, (red, control,))
    while True:
        await asyncio.sleep(60)

asyncio.run(my_app())  # Run main application code

You can always see the video to convince yourself that this is actually working 😁.

Bistable Schematic

OK, what about bistable, why did I cross it out??? Well, the schematic as is won’t work because 3.3V supply will not force enough current through the relay to make it change the state. I could do something with 4-6 extra transistors (some NPN, some PNP) to control positive/negative polarity across the relay, but that would require a post on it’s own, and it becomes complex quite quickly.

I did say above that I could use 9V with for monostable relay as is, so why not do that for bistable? Well, here’s slightly adapted schematic:

Schematic showing both workable versions for monostable and bistable relays controlled by pico
Fixed Bistable relay circuit (click for full image)

I highlighted 9V there. The problem with it is that this would fry our Pico GPIO if I did not add additional NPN transistor (as a switch). It acts as sort of a level shifter. And now this will work, but I need additional 9V supply when I already have 5V USB supply.

Turns out, I can scale components a bit and get this to work with 5V supply:

Schematic showing how to control bistable relay with pico
Control bistable relay with 5V supply (click for full image)

I hope this makes sense. In the schematic I just removed R4 and that ensured enough current got through the relay to change it’s state.

Software? Exactly the same as for the monostable version.

Action

Here’s a quick demo of what I covered:

Over next few months (depending on when you’re reading this, it may have happened already), I’ll be occasionally adding new functionality. Subscribe so you don’t miss out 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.