Arduino Code and Video for Solid-State Relay to Control AC Bulb or Load

Arduino Code and Video for Solid-State Relay to Control AC Bulb or Load

In this tutorial, we will explore how to use a solid-state relay (SSR) to control an AC or DC load, such as a light bulb. The solid-state relay allows for safe and efficient switching of electrical loads using a low-voltage signal from an Arduino. By the end of this tutorial, you will have a working setup that can turn the light on and off at specified intervals.

single channel solid state relay

To implement this project, we'll be using an SSR, specifically the G3MB-202P model, which can handle loads up to 2A. The relay will be controlled by a digital pin from the Arduino board, allowing us to send a signal that turns the load on or off. This project is not only practical but also serves as a great introduction to working with relays and Arduino.

For a detailed visual explanation, be sure to check out the accompanying video (in video at 00:00).

Hardware Explained

In this project, the primary component is the G3MB-202P solid-state relay. This relay allows you to control a high-voltage load with a low-voltage signal. It operates by using an opto-isolator to switch the load circuit, providing electrical isolation between the control and load sides. The relay features three pins: DC+, DC-, and the input channel, which connects to the Arduino.

When the input signal is low (0V), the relay activates and allows current to flow through the load. Conversely, when the input signal is high (5V), the relay turns off, cutting the current to the load. This low-trigger mechanism is essential for safely managing high-voltage devices without direct electrical contact.

Datasheet Details

ManufacturerOmron
Part numberG3MB-202P
Logic/IO voltage5 V
Supply voltage5–24 V
Output current (per channel)2 A
Peak current (per channel)4 A
PWM frequency guidanceUp to 1 kHz
Input logic thresholdsLow: 0–1.5 V, High: 3–30 V
Voltage drop / RDS(on) / saturation1.5 V max
Thermal limits–30 to 100 °C
PackageThrough-hole
Notes / variantsAvailable in various input voltage ratings

  • Ensure the relay is rated for the load you intend to control.
  • Use proper heat-sinking if operating near the maximum current rating.
  • Double-check wiring to avoid reversed polarity issues.
  • Utilize decoupling capacitors on the power supply lines.
  • Beware of floating inputs; ensure the control signal is correctly grounded.
  • Confirm PWM frequency is within relay specifications for switching applications.

Wiring Instructions

Arduino wiring for single channel solid state relay
Arduino wiring for single channel solid state relay

Wiring the solid-state relay is straightforward. Connect the positive terminal of the relay (DC+) to the 5V pin on the Arduino. The negative terminal (DC-) should be connected to the ground (GND) of the Arduino. The input pin, which controls the relay, connects to digital pin 8 on the Arduino. This pin will send the low or high signal to activate or deactivate the relay.

On the load side, connect one wire from the AC bulb to the relay output terminal and the other wire to the AC supply. Make sure to use appropriate connectors and ensure that all connections are secure to prevent any electrical hazards.

Code Examples & Walkthrough

Here is a brief overview of the Arduino code to control the solid-state relay:

int relayPin = 8; // set pin 8 for relay output

void setup() {
    Serial.begin(9600); // initialize serial communication
    pinMode(relayPin, OUTPUT); // set relay pin as output
}

In this excerpt, we define relayPin as 8, which corresponds to the digital pin connected to the relay. The setup() function initializes the serial communication and sets the relay pin as an output.

void loop() {
    digitalWrite(relayPin, LOW); // turn relay ON
    Serial.println("Relay ON"); // output to serial monitor
    delay(2000); // wait for 2 seconds
}

In the loop() function, we turn the relay on by setting relayPin to LOW, which activates the relay and allows current to flow to the load. A message is printed to the serial monitor for confirmation.

    digitalWrite(relayPin, HIGH); // turn relay OFF
    Serial.println("Relay OFF"); // output to serial monitor
    delay(2000); // wait for 2 seconds
}

Next, we turn off the relay by setting relayPin to HIGH, which stops the current flow to the load. Again, we print a message to the serial monitor to indicate the relay status. This process repeats indefinitely, turning the relay on and off every two seconds.

For the complete Arduino code, please see the full code loaded below the article.

Demonstration / What to Expect

When the code is uploaded to the Arduino, you should see the relay clicking on and off, turning the connected bulb on and off every two seconds. If the relay does not function as expected, check the wiring connections and ensure that the relay is receiving the correct control signal. Common issues include reversed polarity on the load side or incorrect connections on the Arduino.

For further visual confirmation of the relay operation, refer to the video (in video at 06:00) where the bulb is shown turning on and off as the relay activates.

Video Timestamps

  • 00:00 - Introduction to the project
  • 01:30 - Hardware setup
  • 03:15 - Code explanation
  • 05:45 - Live demonstration
  • 07:00 - Conclusion

Images

single channel solid state relay
single channel solid state relay
single channel solid state relay
single channel solid state relay
Arduino wiring for single channel solid state relay
Arduino wiring for single channel solid state relay
59-This is the Arduino code for a 5V solid-state relay.
Language: C++
Copied!

Resources & references

No resources yet.

Files📁

Fritzing File