Arduino MicroPython Relay

This tutorial teaches you how to use an Arduino and MicroPython to control a relay module. Specifically, we will learn:

Arduino MicroPython and relay

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Relay
1×LED Strip
1×12V Power Adapter
1×DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Mega/Giga
1×(Recommended) Enclosure For Arduino Giga
1×(Recommended) Power Splitter For Arduino Giga

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Relay

A relay is a programmable switch that can be controlled by an Arduino or other microcontrollers. It allows automated control of devices, especially those that operate with high voltage or high current, by turning them on or off. In essence, the relay serves as a bridge between the Arduino and high-voltage devices.

WARNING

Safety is essential when working with electricity, so be cautious to avoid electric shocks. If you are unsure about anything, seek assistance from someone experienced.

We recommend using a DC device (up to 24V) for testing purposes, even though many relays are compatible with both DC and AC devices.

Relay Pinout

Relay Pinout

A relay has two groups of pins: input pins, which interfaces with low voltage, and output pins, which interfaces with high voltage.

  • Input Pins: Connect these to the Arduino. There are three input pins:
    • DC- pin: Connect to GND (0V).
    • DC+ pin: Connect to VCC (5V).
    • IN pin: Connect this pin to receive the control signal from the Arduino.
  • Output Pins: Connect these to the high-voltage device. These pins are typically arranged in a screw terminal, and there are three:
    • COM pin: Common connection used in both the normally open (NO) and normally closed (NC) modes.
    • NO pin: Normally open pin. Use this in the normally open setting.
    • NC pin: Normally closed pin. Use this in the normally closed setting.

    Usually, only two of the output pins are used, depending on the mode:

    • Normally Open Mode: Connect the COM pin and the NO pin.
    • Normally Closed Mode: Connect the COM pin and the NC pin.

    If the relay supports both LOW level trigger and HIGH level trigger, there is typically a jumper to select either the LOW level trigger or the HIGH level trigger.

    We will cover the details of LOW level trigger, HIGH level trigger, normally open mode, and normally closed mode at the end of this tutorial. To keep things simple initially, we will use the HIGH level trigger and normally open mode.

    ※ NOTE THAT:

    Different manufacturers may arrange the pins on the relay module in various ways. Always carefully check the labels on your relay to ensure the correct pin connections. Be attentive to the pin placements!

    How to Connect the High Voltage Device to Relay

    How to connect relay

    Arduino - Relay

    The Arduino uses a relay to manage a device that works with high voltage.

    Controlling a relay is simple. We only need:

    • Connect the pin on the Arduino to the IN pin on the relay.
    • Set the pin to LOW or HIGH to control the relay.

Wiring Diagram

The wiring diagram between Arduino MicroPython Relay

This image is created using Fritzing. Click to enlarge image

How To Program For Relay

  • To configure a Arduino pin as a digital output. For example, to set up pin 3, use this function.
relay = Pin(RELAY_PIN, Pin.OUT)
  • Turn on relay.
relay.value(1) # Set relay to HIGH (relay ON)
  • Turn off relay.
relay.value(0) # Set relay to LOW (relay OFF)

Arduino MicroPython Code

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-relay """ from machine import Pin import time RELAY_PIN = 'D5' # The Arduino Giga WiFi pin D5 connected to the relay pin # Create a relay object using the Pin class relay = Pin(RELAY_PIN, Pin.OUT) # Main loop to toggle the relay while True: relay.value(1) # Set relay to HIGH (relay ON) time.sleep(2) # Delay for 2 seconds relay.value(0) # Set relay to LOW (relay OFF) time.sleep(2) # Delay for 2 seconds

Detailed Instructions

Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the Arduino board to the relay according to the provided diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny\'s editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • Check out the relay state.

Video Tutorial

Advanced Use: How Relay Works

Depending on the manufacturer and the user's setup, a relay can operate differently.

Input Mode (for IN pin): There are two input modes that cause the relay to function oppositely:

  • LOW level trigger mode
  • HIGH level trigger mode

Output Mode (for output pins): There are two output modes that also cause the relay to function oppositely:

  • Normally open mode
  • Normally closed mode

The term "normally" refers to the state “when the IN pin is connected to LOW (0V)”.

Before diving deeper, here are some quick points:

  • The normally open and normally closed modes work oppositely.
  • Most relay modules support both normally open and normally closed modes.
  • The LOW level trigger and HIGH level trigger modes work oppositely.
  • Not all relay modules support both LOW level trigger and HIGH level trigger modes.
  • At any time, the relay module can operate in only one of the two modes: LOW level trigger or HIGH level trigger.

Combining the input modes and output modes creates various use cases. If you are a beginner, it is recommended to use the HIGH level trigger mode with the normally open mode.

Since the LOW level trigger and HIGH level trigger modes function oppositely, the next section will explain the HIGH level trigger mode in detail. The LOW level trigger mode works in the opposite way.

HIGH Level Trigger - Normally Open Mode

To use this mode, connect the high-voltage device to both the COM pin and the NO pin:

  • When the IN pin is connected to LOW (0V), the switch remains open, and the device stays OFF.
  • When the IN pin is connected to HIGH (5V), the switch closes, and the device turns ON.
How Relay Works - Normally Open

HIGH Level Trigger - Normally Closed Mode

To use this mode, connect the high-voltage device to the COM pin and the NC pin:

  • When the IN pin is connected to LOW (0V), the switch is closed, and the device is turned ON.
  • When the IN pin is connected to HIGH (5V), the switch is open, and the device is turned OFF.
How Relay Works - Normally Closed

Summary

The table below shows the relationship between input settings and output states.

Input modes Output modes IN pin (programmable) Output pins Relay state Device state
HIGH Trigger Normally Open LOW COM and NO pin ⇒ open OFF
HIGH Trigger Normally Open HIGH COM and NO pin ⇒ closed ON
HIGH Trigger Normally Closed LOW COM and NC pin ⇒ closed ON
HIGH Trigger Normally Closed HIGH COM and NC pin ⇒ open OFF
LOW Trigger Normally Open LOW COM and NO pin ⇒ closed ON
LOW Trigger Normally Open HIGH COM and NO pin ⇒ open OFF
LOW Trigger Normally Closed LOW COM and NC pin ⇒ open OFF
LOW Trigger Normally Closed HIGH COM and NC pin ⇒ closed ON

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!