Close Menu
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
Facebook X (Twitter) Instagram
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us
Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
How To Electronics
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
How To Electronics
Home » IoT Controlled Relay using Raspberry Pi Pico W WiFi
Raspberry Pi Raspberry Pi Pico W Projects

IoT Controlled Relay using Raspberry Pi Pico W WiFi

Mamtaz AlamBy Mamtaz AlamUpdated:May 28, 20234 Comments5 Mins Read
Share Facebook Twitter LinkedIn Telegram Reddit WhatsApp
IoT Controlled Relay using Raspberry Pi Pico W WiFi
Share
Facebook Twitter LinkedIn Pinterest Email Reddit Telegram WhatsApp

Overview

In this project, we will build an IoT Relay using Raspberry Pi Pico W WiFi and control the Relay via a Web Server. We will use Thonny IDE to program the Raspberry Pi Pico W using MicroPython Code. It’s a great way to build a Wi-Fi switch to turn on and off any electrical device spending very little money.

This DIY IoT Relay based on the Raspberry Pi Pico W WiFi + Bluetooth module can be used to control High Power Devices like Water Pumps, Air Conditioners, Heaters, and other High Ampere loads. The best thing about this IoT Relay is that it can be controlled remotely in a local network using WiFi technology. The cell phone or any web application can be used to access the Web Server. Using the web page, you can turn ON/OFF any appliances at your home.


Bill of Materials

To control the Relay using the Raspberry Pi Pico W Web server, we need the following components. You can purchase them from the following links.

S.N.ComponentsQuantityPurchase Link
1Raspberry Pi Pico W1Amazon | AliExpress
21 Channel Relay1Amazon | AliExpress
3AC Bulb1-
4Breadboard1Amazon | AliExpress
5Connecting Wires1Amazon | AliExpress
6Micro-USB Cable1Amazon | AliExpress



Relays & Relay Module

A relay module is an electrical switch that is operated by an electromagnet. The electromagnet is activated by a separate low-power signal from a microcontroller. When activated, the electromagnet pulls to either open or close an electrical circuit.

The relay can be activated via 3.3V or 5V of a microcontroller like Raspberry Pi Pico W. This allows to control of appliances operating at 12V, 24V, 110V, or 220V AC.

There are single-channel or multiple-channel relays available in the market. The multiple-channel relay includes 1, 2, 4, 8, 16 Channels.

Some relay module comes with a built-in optocoupler that optically isolate the Microcontroller boards from the relay circuit and protect the microcontrollers.

Most of the Relay Module has 6 pins, 3 on each side. The VCC, GND & Input pin is used with Microcontroller. The NO, Common & NC is used with High Voltage Electrical Appliances.

Pin Number Pin Name Description
1 Relay Trigger Input to activate the relay
2 Ground 0V reference
3 VCC Supply input for powering the relay coil
4 Normally Open Normally open terminal of the relay
5 Common Common terminal of the relay
6 Normally Closed Normally closed contact of the relay




Circuit Diagram & Connections

Let us see how we can connect the single-channel Relay Module to Raspberry Pi Pico W. The connection diagram is fairly simple.

Raspberry Pi Pico W IoT Relay

Connect the VCC, GND & Input pin of the Relay to 3.3V, GND & GP18 of Pi Pico W. Connect any electrical appliances like a fan or bulb at the output of the Relay Module. The electrical appliances work at 110/220V.

IoT Relay Raspberry Pi Pico W

In this example, we’re controlling a lamp & it is advised to use a normally open configuration as in the image above.


Project PCB Gerber File & PCB Ordering Online

If you don’t want to assemble the circuit on a breadboard and you want PCB for the project, then here is the PCB for you. I used EasyEDA to draw the schematic first.

Raspberry Pi Pico W Relay Schematic

Then I converted the schematic to PCB. The PCB Board for this project looks something like below.

The Gerber File for the PCB is given below. You can simply download the Gerber File and order the PCB from ALLPCB at 1$ only.

Download Gerber File: Raspberry Pi Pico W Relay Control

You can use this Gerber file to order high quality PCB for this project. To do that visit the ALLPCB official website by clicking here: https://www.allpcb.com/.

You can now upload the Gerber File by choosing the Quote Now option. From these options, you can choose the Material Type, Dimensions, Quantity, Thickness, Solder Mask Color and other required parameters.

After filling all details, select your country and shipping method. Finally you can place the order.

You can assemble the components on the PCB Board.


MicroPython Code to Control a Relay Module

The following code is used to control a relay with the Raspberry Pi Pico W. It is as simple as controlling an LED or any other output. We will use a normally open configuration.

When we send a LOW signal to GP18, the Relay will turn ON. Similarly sending a HIGH signal to GP18 will turn OFF the relay.

1
2
3
4
5
6
7
8
9
10
from machine import Pin
import utime
 
relay=Pin(18,Pin.OUT)        
 
while True:
  relay.value(1)            #Set relay turn on
  utime.sleep(5)
  relay.value(0)            #Set relay turn off
  utime.sleep(5)

After running the code, you will see Relay turning ON and OFF after every 5 seconds.


MicroPython Code to Control a Relay Module via Web Server

In this MicroPython Code, we’ve created a Web Server script that allows you to control a relay remotely using a Raspberry Pi Pico W Local IP Address & web page.

Before running the code, from the following lines change the WiFi SSID and password.

1
wlan.connect("***********","***********")       # ssid, password

Save the code to Raspberry Pi Pico W memory and give it any name like main.py.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from machine import Pin
import network
import time
try:
  import usocket as socket
except:
  import socket
 
relay=Pin(18,Pin.OUT)
 
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("***********","***********")       # ssid, password
 
# connect the network      
wait = 10
while wait > 0:
    if wlan.status() < 0 or wlan.status() >= 3:
        break
    wait -= 1
    print('waiting for connection...')
    time.sleep(1)
 
# Handle connection error
if wlan.status() != 3:
    raise RuntimeError('wifi connection failed')
else:
    print('connected')
    ip=wlan.ifconfig()[0]
    print('IP: ', ip)
    
 
def web_server():
  if relay.value() == 1:
    relay_state = ''
  else:
    relay_state = 'checked'
  html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1"><style>
  body{font-family:Arial; text-align: center; margin: 0px auto; padding-top:30px;}
  .switch{position:relative;display:inline-block;width:120px;height:68px}.switch input{display:none}
  .slider{position:absolute;top:0;left:0;right:0;bottom:0;background-color:#ccc;border-radius:34px}
  .slider:before{position:absolute;content:"";height:52px;width:52px;left:8px;bottom:8px;background-color:#fff;-webkit-transition:.4s;transition:.4s;border-radius:68px}
  input:checked+.slider{background-color:#2196F3}
  input:checked+.slider:before{-webkit-transform:translateX(52px);-ms-transform:translateX(52px);transform:translateX(52px)}
  </style><script>function toggleCheckbox(element) { var xhr = new XMLHttpRequest(); if(element.checked){ xhr.open("GET", "/?relay=on", true); }
  else { xhr.open("GET", "/?relay=off", true); } xhr.send(); }</script></head><body>
  <h1>Pico W IoT Relay Control</h1><label class="switch"><input type="checkbox" onchange="toggleCheckbox(this)" %s><span class="slider">
  </span></label></body></html>""" % (relay_state)
  return html
 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)
 
while True:
  try:
    conn, addr = s.accept()
    conn.settimeout(3.0)
    print('Got a connection from %s' % str(addr))
    request = conn.recv(1024)
    conn.settimeout(None)
    request = str(request)
    print('Content = %s' % request)
    relay_on = request.find('/?relay=on')
    relay_off = request.find('/?relay=off')
    if relay_on == 6:
      print('RELAY ON')
      relay.value(0)
    if relay_off == 6:
      print('RELAY OFF')
      relay.value(1)
    response = web_server()
    conn.send('HTTP/1.1 200 OK\n')
    conn.send('Content-Type: text/html\n')
    conn.send('Connection: close\n\n')
    conn.sendall(response)
    conn.close()
  except OSError as e:
    conn.close()
    print('Connection closed')



Testing IoT Relay using Raspberry Pi Pico W Web Server

After running the Code, the Thonny Shell will display the local IP Address of Raspberry Pi Pico W.

Copy the IP Address and paste it on any Web Browser. A page will appear as shown below.

IoT Relay Web Server

You can slide the switch from the off position on the web page, and the relay will turn on immediately. In this case, we are making an HTTP request on GET /?relay=on HTTP/1-IP-address/on. This request returns a webpage with the changed GP16 state on it.

Raspberry Pi Pico W relay Web Server

Sliding the switch again from the on position to the off position will turn off the relay again. In this case. we are making a request on the Pico W IP address, followed by /?GET /?relay=off The LED turns off, and the GP16 state is updated.

Raspberry Pi Pico W relay Control

The Thonny Shell window will display the following message.

This is the easiest method to control any home appliances via a Relay module using Raspberry Pi Pico W Web Server.

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit Telegram WhatsApp
Previous ArticleBuild a DIY Customized Mini PC using Raspberry Pi 4
Next Article IoT Temperature Based Fan Speed Control & Monitoring System

Related Posts

ADXL375 Accelerometer with Raspberry Pi Pico & MicroPython

ADXL375 Accelerometer with Raspberry Pi Pico & MicroPython

Updated:July 24, 2025
Interface BMI160 with Raspberry Pi Pico & MicroPython

Interface BMI160 with Raspberry Pi Pico & MicroPython

Updated:February 2, 20253K
Shift Register 74HC595 with Raspberry Pi Pico & MicroPython

Shift Register 74HC595 with Raspberry Pi Pico & MicroPython

Updated:February 2, 202513K
Interfacing XBee Module with Raspberry Pi Pico & MicroPython

Interfacing XBee Module with Raspberry Pi Pico & MicroPython

Updated:February 2, 20253K
Modbus RTU with Raspberry Pi Pico & Micropython

Modbus RTU with Raspberry Pi Pico & MicroPython

Updated:February 2, 20258K
Fever Detector with MLX90640 & OpenCV Raspberry Pi

Thermal Fever Detector with MLX90640 & OpenCV Raspberry Pi

Updated:February 2, 20256K
View 4 Comments

4 Comments

  1. Kayu on April 8, 2023 10:33 AM

    Thanks for the guide! Do all relays support 3.3v or do you need to find one specifically for that? I read somewhere that some relay may not get enough power with 3.3 to energize the coils…

    Reply
    • Admin on April 8, 2023 10:33 AM

      Using a transistor you can turn any relay whether 5v ir 3.3V.

      Reply
    • Mag on January 15, 2024 5:35 PM

      I tried this project and it worked great. Do you have any example on how to create two buttons slide to turn on two different relays?

      Reply
  2. Subhrajeet Pradhan on January 5, 2024 11:26 PM

    I have the same problem to connect the relay module to Wi-Fi. But, can i get the same code which can run on Arduino ide.

    Thank you

    Reply

CommentsCancel reply

Latest Posts
IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

May 31, 2026
DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

May 10, 2026
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

May 2, 2026
A Guide to Sourcing Obsolete ICs for Vintage Projects

Beyond AliExpress: A Guide to Sourcing Obsolete ICs for Vintage Projects

April 21, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

April 27, 2026
Building a Smart Sensor Node with a BLE Microcontroller

Building a Smart Sensor Node with a BLE Microcontroller

February 26, 2026
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

April 27, 2026
DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

February 1, 2026
Top Posts & Pages
  • 12V DC to 220V AC Inverter Circuit & PCB
    12V DC to 220V AC Inverter Circuit & PCB
  • IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
    IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
  • IoT Based Drinking Water Quality Monitoring with ESP32
    IoT Based Drinking Water Quality Monitoring with ESP32
  • LD2410 Sensor with ESP32 - Human Presence Detection
    LD2410 Sensor with ESP32 - Human Presence Detection
  • ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
    ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
  • DIY IoT Water pH Meter using pH Sensor & ESP32
    DIY IoT Water pH Meter using pH Sensor & ESP32
  • Buck Converter: Basics, Working, Design & Application
    Buck Converter: Basics, Working, Design & Application
  • How to use Modbus RTU with ESP32 to read Sensor Data
    How to use Modbus RTU with ESP32 to read Sensor Data
Categories
  • Arduino Projects (197)
  • Articles (60)
    • Learn Electronics (19)
    • Product Review (15)
    • Tech Articles (28)
  • Electronics Circuits (46)
    • 555 Timer Projects (21)
    • Op-Amp Circuits (7)
    • Power Electronics (13)
  • IoT Projects (204)
    • ESP32 MicroPython (7)
    • ESP32 Projects (81)
    • ESP32-CAM Projects (15)
    • ESP8266 Projects (76)
    • LoRa/LoRaWAN Projects (22)
  • Microcontrollers (38)
    • AMB82-Mini IoT AI Camera (4)
    • BLE Projects (18)
    • STM32 Projects (19)
  • Raspberry Pi (93)
    • Raspberry Pi Pico Projects (57)
    • Raspberry Pi Pico W Projects (12)
    • Raspberry Pi Projects (24)
Follow Us
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
About Us

“‘How to Electronics’ is a vibrant community for electronics enthusiasts and professionals. We deliver latest insights in areas such as Embedded Systems, Power Electronics, AI, IoT, and Robotics. Our goal is to stimulate innovation and provide practical solutions for students, organizations, and industries. Join us to transform learning into a joyful journey of discovery and innovation.

Copyright © How To Electronics. All rights reserved.
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us

Type above and press Enter to search. Press Esc to cancel.

Ad Blocker Enabled!
Ad Blocker Enabled!
Looks like you're using an ad blocker. Please allow ads on our site. We rely on advertising to help fund our site.