
With the release of the Raspberry Pi Pico, a whole host of low cost / low power automation projects are now possible while remaining within the Raspberry Pi ecosystem! This powerful little microcontroller is easy to work with and features MicroPython and Arduino support. To help the Pico reach out and work with the world around it we have created a series of add-on boards that provide power regulation, sensor connections, and additional functionality.
About The Board:
Our Raspberry Pi Pico 1591B Relay Board is the first of several boards designed to make automation using the Pi Pico as simple as possible! This board combines relays, a power regulator, and Qwiic / STEMMA QT connector to provide a quick method of switching higher voltages or currents based on connected sensors, time, remote commands, or other parameters. The 1591B form factor means it will fit in the popular series of Hammond 1591B enclosures.
Relays can be thought of as big electronically controlled switches. Being able to control them with a microcontroller or single board computer is one of the key elements in basic automation – but it isn’t as simple as just connecting a relay to your Raspberry Pi Pico. Powering a relay requires a lot more current than can be provided by a simple digital output from a microcontroller. Also, when the relay coil is de-energized, the reverse voltage spike is particularly harmful to sensitive electronics! So, additional circuitry is required! Our 1591B Relay Board take the required driver circuit and packages it neatly on a single circuit board so all you need to do is plug everything in – no need to worry about transistors, diodes, or any of that!
Since this board is mostly assembled, all that is specifically required is a Raspberry Pi Pico. At the time of writing this tutorial, the choice is simply between a Pico or Pico W. Choosing between the two is quite simple – if you need internet or network connectivity, a Pico W is recommended. If you do not, then a plain Pico will work just fine.
Optionally:
There isn’t much in the way of assembly required for the Relay Board. We will simply need to attach the Raspberry Pi Pico of choice to the Relay Board and then we can jump right to programming.
Similar to the ESP series of microcontrollers, the Raspberry Pi Pico uses castellated edges on the board, allowing this to be surface mounted to the board below. Don’t be thrown off by the surface mount soldering – this is a very easy method of connecting two boards, well suited to conventional soldering irons and solder. No special SMT soldering equipment / reflow / etc. are required.
In the next few steps we will get the Pico attached to the Pico 1591B Relay Board. This is very easy to do, simply line up the board on the outline as shown in the image. We are just going to solder one pin in one corner to ensure everything is lined up as it should.
Don’t solder all the points at once, as adjusting the placement of the board is extremely difficult once several solder pads are filled. This method allows the single point to be reheated and the board moved if adjustment is needed.
If everything is still aligned as it should be, we can now solder all the remaining pads. Your result should be similar to the example image.
After this is complete, inspect your work and ensure there are no bridges between adjacent pads. Once you are satisfied the soldering completed correctly, we can move on to the next step!
Before we power anything up, take another good look at the work that has been done. Make sure all solder joints are clean, nothing looks out of the ordinary, and no components were damaged / moved / removed during the soldering process. Once the board has been thoroughly checked, we can move on.
Time to plug it in and get started! The assembled Pico / Relay Board will act just like a regular Pico when plugged in via USB. In this tutorial we are going to look at both MicroPython (Using Thonny) and Arduino (Using the Arduino IDE). You can choose which ever you are more comfortable with!
If this is a brand new Pico, you will need to flash the bootloader. Below we have links to getting a Pico set up with both Arduino and Thonny.
Once everything is set up, we can move to the next step.
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Since we are looking at two different programming examples, if you are interested in Arduino, we will address that first. If you are more comfortable with Python, skip down to the Python Example.
We are starting with the BareMinimum Sketch found in the IDE, it should look something like this:
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
So first we will need some variables for the Pico GPIO pins we plan to use:
int relayPin1 = 2; //This is the Arduino Pin that will control Relay #1
int relayPin2 = 3; //This is the Arduino Pin that will control Relay #2
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
Basically we are going to reduce the opportunity for confusion later down the road by assigning names to each of the Pico’s pins we use. Now, instead of trying to remember that Pico GPIO Pin 2 is Relay number 1, we will simply just refer to the variable “relayPin1” instead – much easier!
Next we will set all of the pins we are using to be digital outputs:
int relayPin1 = 2; //This is the Arduino Pin that will control Relay #1
int relayPin2 = 3; //This is the Arduino Pin that will control Relay #2
void setup() {
// put your setup code here, to run once:
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
}
Ok, now all of the pins are set to outputs. Next we can write some code in the loop to switch a relay on and off:
int relayPin1 = 2; //This is the Arduino Pin that will control Relay #1
int relayPin2 = 3; //This is the Arduino Pin that will control Relay #2
void setup() {
// put your setup code here, to run once:
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(relayPin1, HIGH); //Switch Relay #1 ON
delay(1000); //Wait 1 Second
digitalWrite(relayPin1, LOW); //Switch Relay #1 OFF
delay(1000); //Wait 1 Second
}
If we were to upload this code to the Pico it will turn Relay #1 on for 1 second and then switch it off for 1 second and continue through this loop forever.
So, before we upload the code let’s use both relays to make a lot of clicking noise:
int relayPin1 = 2; //This is the Arduino Pin that will control Relay #1
int relayPin2 = 3; //This is the Arduino Pin that will control Relay #2
void setup() {
// put your setup code here, to run once:
pinMode(relayPin1, OUTPUT);
pinMode(relayPin2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(relayPin1, HIGH);
delay(1000);
digitalWrite(relayPin1, LOW);
digitalWrite(relayPin2, HIGH);
delay(1000);
digitalWrite(relayPin2, LOW);
}
This is just the most basic code triggering the relays, but it should give you a point to start off from. Going forwards you can use this board’s Qwiic / STEMMA QT interface to connect various sensors or use the Pico W’s WiFi interface to control when these relays are triggered.
Before testing, have a look at our Testing Note below!
We are starting with a blank Python sketch in this example, but we will need to include several libraries.
from machine import Pin
from time import sleep
Next we will need some variables for the Pico GPIO pins we plan to use:
from machine import Pin
from time import sleep
relayPin1 = Pin(2, Pin.OUT)
relayPin2 = Pin(3, Pin.OUT)
Basically we are going to reduce the opportunity for confusion later down the road by assigning names to each of the Pico’s pins we use. Now, instead of trying to remember that Pico GPIO Pin 2 is Relay number 1, we will simply just refer to the variable “relayPin1” instead – much easier!
Next we will create a basic loop (to mimic our Arduino example)
from machine import Pin
from time import sleep
relayPin1 = Pin(2, Pin.OUT)
relayPin2 = Pin(3, Pin.OUT)
while(True):
#Do something forever here
Next we can write some code in the loop to switch a relay on and off:
from machine import Pin
from time import sleep
relayPin1 = Pin(2, Pin.OUT)
relayPin2 = Pin(3, Pin.OUT)
while(True):
#Do something forever here
relayPin1.value(1)
sleep(1)
relayPin1.value(0)
sleep(1)
If we were to upload this code to the Pico it will turn Relay #1 on for 1 second and then switch it off for 1 second and continue through this loop forever.
So, before we save this code to the Pico let’s use both relays to make a lot of clicking noise:
from machine import Pin
from time import sleep
relayPin1 = Pin(2, Pin.OUT)
relayPin2 = Pin(3, Pin.OUT)
while(True):
#Do something forever here
relayPin1.value(1)
sleep(1)
relayPin1.value(0)
relayPin2.value(1)
sleep(1)
relayPin2.value(0)
This is just the most basic code triggering the relays, but it should give you a point to start off from. Going forwards you can use this board’s Qwiic / STEMMA QT interface to connect various sensors or use the Pico W’s WiFi interface to control when these relays are triggered.
If you were to upload our example code (Either Arduino or Python) and test, it will seem like nothing is happening. This is due to the way power is handled on board. Since 12VDC power is not currently connected, there is nothing available to trigger the Relay.
The two pin connector on the side of the board provides power to the Pico by way of an onboard regulator and to the relay coils by way of a control circuit for each relay. Generally this input should be kept within 11-14VDC but it will tolerate 9-15VDC. Any lower and the relays will not have enough power to operate, too high and the relays will start to get too hot.
Disconnect your USB cable and connect the board to a 12VDC power source by way of the VCC/ GND pins in the 2 pin screw terminal. Once this is done, the relays should trigger in an alternating pattern.
On the Opposite side of the board to the power input, we have large screw terminals for the relay contacts. The specific relays we use in our 1591B Relay Boards are a SPDT contact configuration (Single Pole, Double Throw) meaning they have three contact pins: Normally Closed (NC), Normally Open (NO), and Common (COM). The common pin that is connected to one of the other two contact pins depending on the relay’s state.
When the relay is not powered, it is in its “Normal” state. In the normal state the Normally Closed “NC” pin is connected to the Common pin “COM”. Normally Open “NO” is left open (not connected to anything). Powering the relay coil will change the relay state, meaning the “COM” pin is now connected to “NO” and the “NC” pin is no longer connected to anything. Removing power from the relay coil, the relay reverts back to its normal state once again.
Depending on your application, connect your high current wires to the correct contacts on this side of the board.
5 thoughts on “Getting Started With The Raspberry Pi Pico 1591B Relay Board”
gordon
“Disconnect your USB cable and connect the board to a 12VDC power source by way of the VCC/ GND pins in the 2 pin screw terminal”
Chris:
Clarification on the above statement please:
What happens if the 12Volt pwr supply and the USB cable are connected at the same time? Will it cause damage to the Pico board?
Thanks for any help.
Gordon
William @ BC Robotics
Hey Gordon!
Great question – there is no risk of damage since the Pico uses an internal circuit to prevent backfeed up the USB power input. The reason we suggest disconnecting the USB is just best practice for wiring where nothing should be powered while you are working on it 🙂
Gordon
Thanks for the info
Gordon
peterwilliamkohlmann
So I connected the Raspberry Pi Pico W to the Relay board as instructed. Everything worked 100%, until…. I was able to connect to the board using a USB cable and used Thonny to program it. I set up a simple website and tested the indicator LED. All good.
I disconnected the USB as instructed. I then attached a 12V source. + to VCC and = to GND. As soon as I engaged the breaker for the 12V source the chip to the right of the 2 pin screw terminal started smoking. I turn off the 12V supply immediately. Both boards are now dead and unresponsive. I was very careful to ensure no shorts on any of the soldered pin and there was no short between the 12v source connection. I don’t believe I missed anything in the instructions. Was this a case of a bad board relay board? Or is there something missing from the instructions?
William @ BC Robotics
Hello Peter,
That may be a defective board / regulator. Please email us with your order number and some photos of the setup and we can get that all sorted for you.