Summary of DC Motors Relays Using Arduino
This article explains Ohm's Law and power calculations, then details controlling DC motors with an Arduino using relays and H-Bridges. It covers transistor types (NPN/PNP), diode orientation for flyback protection, and provides Processing/Firmata code to toggle a relay via mouse input. It also notes that PWM ports are required for speed control in the second part of the motor project.
Parts used in the DC Motors Relays Using Arduino Project:
- Ardunio board
- Relay module (black cube type)
- NPN or PNP transistor
- Diode
- DC motor
- Battery or external wall wart power supply
- Processing software with Firmata library
Let’s get the math over right off the bat. There are a LOT of attempts to describe how Ohm’s Law works, each one nerdier than the last. Here’ one I like. Warning: SFWBN (safe for work but nerdy).
V = voltage measured in volts – the difference in electrical potential.
I = Current measured in Amps or Amperes – the rate of the flow of electrons.
R = Resistance measured in Ohms (Ω) – opposition to the flow of the current.
V = I * R
I = V / R
R = V / I
It is good to know where “power” fits in also. Power, measured in watts, is the Current (Ampes) multiplied by the Voltage. Mostly you see this equation as W = V * I
Sometimes you see this expressed as a confusing looking wheel. (coincidentally also the coolest tattoo ever)
Oh, and sometimes Volts are expressed with and E instead of a V. They do this because they hate you.
Example: a typical 100 Watt bulb – how many amps does it use? 100 watts / 120 volts = .83 amps
Relays are a great way to separate your low voltage Arduino circuit from a much higher voltage circuits to run motors or lights. Relays are all over – cars, computers they are an electronics staple. Each kind will be wired differently and be able to handle different voltages. we have two different kinds (the black cube ones are more reliable) – they work about the same once you’ve determined the inputs.
This circuit uses a transistor – there are two families of transistor this size, NPN or PNP. You can often swap out different ones from the same family and the circuit will still work fine, but you can’t swap out a PNP for an NPN. Of course they look exactly the same so you have to use the i-tubes to figure out which is which. enjoy.
The diagram does not include the motor circuit. You should be able to hear the actual relay switch turn on and off, but the real test it having it turn on a motor. The photograph does include the diode which helps keep the electricity flowing in the right direction and the relay working consistently. Diodes have a a correct orientation – in this case the white line on the diode goes towards the +5volt.
Simple code for the relay. Requires the Arduino loaded with Firmata.
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int transistorPin = 5;
void setup()
{
size(255, 255);
arduino = new Arduino(this, Arduino.list()[0], 57600);
arduino.pinMode(transistorPin, Arduino.OUTPUT);
}
void draw()
{
if ( mousePressed == true) {
arduino.digitalWrite(transistorPin, Arduino.HIGH);
}
else {
arduino.digitalWrite(transistorPin, Arduino.LOW);
}
}
There’s a small deviation from the diagram – the wire going into pin 4 should go in a PWM port for the analog example to work right – the examples below have it on pin 5.
With an H-Bridge (or in our case with a Dual H Bridge) you can control a DC motor’s sped and direction. This is awesome. In the diagram below the motor is running off a battery. You can use an external wall wart power supply if you want instead.
For more detail: DC motors Relays Usung Arduino
- How do you calculate current if you know power and voltage?
Divide the watts by the volts. - What is the purpose of a relay in this circuit?
To separate low voltage Arduino circuits from higher voltage circuits running motors or lights. - Can you swap an NPN transistor for a PNP transistor in this setup?
No, you cannot swap a PNP for an NPN even though they look identical. - Which direction should the white line on the diode face?
The white line goes towards the positive 5 volt connection. - What port must be used to control motor speed with an analog example?
A PWM port must be used instead of a standard digital pin like pin 4. - Does the provided diagram include the motor circuit?
No, the diagram does not include the motor circuit itself. - Why might Volts be expressed with an E instead of a V?
The text humorously suggests it is done because some people hate you. - What software is required to run the simple relay code?
The code requires the Arduino loaded with Firmata and the Processing environment.