Switch Tutorial Part 1 - Turn on and off a Relay

In this section we’ll walk through creating a Sinric Pro Switch for ESP32, ESP8266 or Raspberry Pi Zero W and then control via Alexa, Google Home or SmartThings.

Prerequisites :

  1. ESP32, ESP8266 or RaspPi W x 1.
  2. SPDT Relay controller x 1.
  3. Jumper Wires.

Sinric Pro esp8266 esp32 picow

Quick introduction to Relay

A relay is an electrically operated switch that can be turned on or off by a low-voltage signal. It is like a regular switch in that it allows current to flow through or not, but it can be controlled by a much smaller voltage. This makes relays ideal for MCUs where it is necessary to control a high-voltage circuit with a low-voltage signal.

How it works

The operation of a relay can vary depending on the manufacturer, the wiring, and the type of relay controller being used. Some relay controllers require a high-level signal from the MCU to activate the relay, while others require a low-level signal. It is important to check the specifications of the relay controller before using it to ensure that it is compatible with your MCU.

High-level signal: A high-level signal is a signal that is above a certain voltage threshold. For example, a 5V relay controller may require a signal that is above 3.3V to activate the relay.

digitalWrite(pin, HIGH);

Low-level signal: A low-level signal is a signal that is below a certain voltage threshold. For example, a 5V relay controller may require a signal that is below 0.8V to activate the relay.

digitalWrite(pin, LOW);

High Voltage Connectors High Voltage Connectors

The relay module in the above has a single connector with three sockets: Common (COM), Normally Closed (NC), and Normally Open (NO).

Common (COM): COM is the common connection for both the NO and NC pins. Connect the high voltage device to the COM pin.

Normally Closed (NC): The circuit is closed by default.

Normally Open (NO): The circuit is open by default.

Normally open (NO) and normally closed (NC) contacts allow you to use one pin to control two separate circuits depending on your requirements. For example, you could use one pin to control the power to a light bulb and another pin to control the power to a motor. When the pin is high/low the light bulb will turn on otherwise the motor will turn on.

Wiring

Sinric Pro esp8266 relay wiring

Sinric Pro esp8266 relay wiring

MCU Pin
ESP32 16
ESP8266 12 (D6)
RaspPi W 6

Before we integrate with Sinric Pro, it is important to verify that the relay is wired correctly. You can use the following code to check whether the relay turns on and off every 5 seconds.

Step 1 : Create a new device in Sinric Pro

Sinric Pro create device alexa

Sinric Pro online offline push notifications

To receive push notifications when your device goes online or offline, enable the “Connect and Disconnect” option in the Sinric Pro app. To receive push notifications when your device turns on or off, enable the “On and Off” option.

Sinric Pro copy device id

Step 2 : Coding

2.1 Install Sinric Pro Library

Sinric Pro install SinricPro library

You can generate the code using Zero Code feature or write it by your self. If you do not have programming experice, we recommend to use Zero Code feature in the Portal to generate the code, download and flash.

2.2 Complete Code

Now you should be able to control the relay via Portal like in the below video.

• Open your Amazon Alexa app.

• Goto Skills & Games.

• Search for Sinric Pro.

• Click ENABLE TO USE.

• Enter your Sinric Pro credentails in linking page.

• Ask Alexa to discover new devices.

• Ask Alexa to turn on the Switch

• Open Google Home and click on Add.

• Select Setup Device

• Select Works with Google Home

• Search for Sinric Pro.

• Link your account.

• Discover new devices

• Ask Google Home to turn on the Switch

• Open the SmartThings app and tap the + Icon

• Select Device

• Select Sinric Pro under By Brand

• Link your account.

• Now you can turn on or off Switch from the dashboard

Continue to Part 2 of this article series to learn how to add a push button to toggle the relay.

Troubleshooting

Please refer to our Troubleshooting page for possible solutions to your issue.

This document is open source. See a typo? Please create an issue