BC Robotics

Getting Started With The Raspberry Pi Pico 1591B Relay Board

  PRODUCT TUTORIAL

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. 

How It Works

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! 

The Parts Needed

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. 

  1 x Raspberry Pi Pico - or - Raspberry Pi Pico W
  1 x USB micro Cable
  1 x Raspberry Pi Pico 1591B Relay Board
  Solder + Soldering Iron
  9-15VDC Power Source

Optionally:

  1 x Hammond 1591B Enclosure
  4 x 4/40 x 1/4" Screws

Step 1 - A Quick Overview

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.  

12.5%

Step 2 - Soldering The Pico (Part 1)

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.  

We are going to start by soldering pin 1 in the corner. Carefully fill the pad with solder and allow it to flow between the Pico and the Relay Board. Your end result should be similar to the pad shown in our example photo.

25%

Step 3 - Soldering The Pico (Part 2)

Once the alignment is good, solder an opposite corner to ensure everything is locked in place. In this case, the opposite pin is 21, close to the relays.

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!

3 37.5%

Step 4 – Double Check Your Work!

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. 

50%

Step 5 - Getting Started With Programming

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. 

Arduino Setup

MicroPython Setup

Once everything is set up, we can move to the next step. 

62.5%

Step 6 - Starting The Code

				
					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

75%

Arduino 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!

Python Example

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.

Step 7 - Powering The Board

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.  

87.5%

Step 8 - Connecting To The Relay(s) - Contacts

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. 

100%

7 thoughts on “Getting Started With The Raspberry Pi Pico 1591B Relay Board”

Leave a Reply

Your email address will not be published.

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the comparison bar
Compare