Arduino with Relay, How To?

Controlling actuators such as motor using an Arduino can be a simple task with the right components. Using relay we can avoid any damage to the microcontroller, the actual and other electronics circuits component in the system. In this guide, we will discuss how to control a motor using a relay and a transistor with an Arduino microcontroller board. This setup allows you to switch the motor on and off using the Arduino, enabling automation and remote control capabilities.

motor control with relay and arduino


Components Needed

- Arduino (any model)
- A small DC motor
- External power supply suitable for the motor
- External power supply for the Arduino (9 volts recommended)
- A DPDT relay with a 5-volt coil rated for 2 amps or more
- A 2N2222 NPN transistor
- A 1N4001/1N4003/1N4007 diode
- A small breadboard
- Connecting wires

Understanding the Components

1.Relay: A relay is an electrically operated switch. It has a coil that, when energized, moves the contact from one position to another. For this project, we use a DPDT relay, which is essentially two SPDT relays in one package, making it easier to connect on a breadboard.
   
2. Transistor: The transistor used here is a 2N2222 NPN transistor, which acts as a switch to control the relay. It has three terminals: collector, base, and emitter. By applying a small voltage to the base, current flows from the collector to the emitter, energizing the relay coil. However you can also use other similar rated bipolar transistor such the BC507, 2N3904 or MOSFET transistor such as the

3.Diode: The 1N4001/1N4003 diode is used for protection. When the relay coil is de-energized, it can generate high voltage spikes that could damage other components. The diode helps to safely dissipate these spikes.

Wiring the Circuit

 The circuit diagram below shows how to connect the Arduino with the relay, motor, transistor and the diode. 

Arduino with Relay

1. Transistor Connection:
   - Connect the emitter of the 2N2222 transistor to the ground.
   - Connect the base to a digital output pin on the Arduino (e.g., pin 13) through a 1kΩ resistor.
   - Connect the collector to one end of the relay coil.

2. Relay Connection:
   - Connect the other end of the relay coil to the positive terminal of the external power supply (5V).
   - Connect the common terminals (COM) of the relay to the motor and the power supply.
   - Connect the normally closed (NC) and normally open (NO) terminals of the relay to control the motor.

3. Diode Connection:
   - Place the diode across the relay coil, with the cathode connected to the positive terminal and the anode to the collector of the transistor. This arrangement ensures the diode will protect against voltage spikes.

Arduino Sketch

Here's a simple sketch to turn the motor on and off in a loop:

int transistorBasePin = 13;

void setup() {
  pinMode(transistorBasePin, OUTPUT);
}

void loop() {
  digitalWrite(transistorBasePin, LOW); // Turns the transistor off
  delay(5000); // Motor stays off for 5 seconds
  digitalWrite(transistorBasePin, HIGH); // Turns the transistor on
  delay(5000); // Motor stays on for 5 seconds
}

Explanation:
-Setup: Configures the transistor base pin as an output.
- Loop: Alternates between turning the transistor on and off every 5 seconds. When the transistor is on, it energizes the relay coil, switching the motor on. When the transistor is off, the relay coil is de-energized, and the motor is turned off.

Testing the Circuit:
1. Assemble the circuit on a breadboard as per the connections described.
2. Upload the sketch to your Arduino.
3. You should observe the motor switching on for 5 seconds and then off for 5 seconds, repeatedly.

Demonstration of how relay with Arduino works, flow of current and voltage is shown in the following video:


Troubleshooting Tips:
- Ensure all connections are secure and correct according to the schematic.
- Double-check the orientation of the transistor and diode.
- Verify the external power supply voltage matches the relay and motor requirements.

This setup allows for simple motor control using an Arduino, relay, and transistor. It forms the basis for more complex motor control projects, including speed and direction control, which can be achieved with additional components and modifications to the sketch.

Further Ideas

 

Post a Comment

Previous Post Next Post