Arduino Control Relay - Tutorial #5

6

Click here to load reader

description

Arduino Control Relay - Tutorial #5

Transcript of Arduino Control Relay - Tutorial #5

Page 1: Arduino Control Relay - Tutorial #5

questions & answers

Comments

Members

Links

Advertise

Contact

Register

Login

555

Alarms

Arduino

Audio

Basic

DIY

Hobby

Lights

Measure

Power supply

Radio

Solar

Theory

Tips

Various »

Tools »

looking for something?

Control a Relay with Arduino – Tutorial #5

Posted by Popescu Marian in Arduino with 2 comments

Tagged with: arduino tutorial•digital

In this quick Arduino tutorial I will explain how you can control a relay using the Arduino Board, one 1K and one 10K

resistors, 1 BC547 transistor, one 6V or 12V relay, one 1N4007 diode and a 12V fan. When the button is pressed the fan will

turn ON and will remain in this state until the button is pressed again.

Arduino Relay Sketch

download Arduino sketch

/* sketch

turn on a fan using a relay and a button

*/

int pinButton = 8;

int Relay = 2;

int stateRelay = LOW;

int stateButton;

int previous = LOW;

long time = 0;

long debounce = 500;

Arduino Control Relay - Tutorial #5 http://www.electroschematics.com/8975/arduino-control-relay/

1 of 6 9/14/2013 3:29 PM

Page 2: Arduino Control Relay - Tutorial #5

void setup() {

pinMode(pinButton, INPUT);

pinMode(Relay, OUTPUT);

}

void loop() {

stateButton = digitalRead(pinButton);

if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {

if(stateRelay == HIGH){

stateRelay = LOW;

} else {

stateRelay = HIGH;

}

time = millis();

}

digitalWrite(Relay, stateRelay);

previous == stateButton;

}

Arduino Control Relay Schematic

How does the circuit works

When the button is pressed the Arduino board will put pin 2 in HIGH state, meaning 5V on pin 2. This voltage is used to

drive the transistor that will switch ON the relay and the load (in our case the fan) will be powered from the main power

supply.

You cannot use the 5V from the USB to power up the transistor and the LOAD because the USB port usually delivers only

100mA, and this is not enough to switch the relay and the LOAD. That is why you must use an external power supply (Vcc)

that is between 7 to 12 volts to power up the Arduino board and the transistor + relay. The load uses its own power supply,

for instance if you use a light bulb then you might connect it to the 110/220V mains or any other power source.

Arduino Control Relay - Tutorial #5 http://www.electroschematics.com/8975/arduino-control-relay/

2 of 6 9/14/2013 3:29 PM

Page 3: Arduino Control Relay - Tutorial #5

DO NOT connect in any ways the main power supply that drive the LOAD to the arduino and transistor circuitry!

Turn OFF the relay with delay

You can use this code example to introduce a delay in your circuit. The variable “stayON” is used to delay() the program

execution with the desired amount of time. In our case after the button is pressed the relay will be switched ON and after 5

seconds will the turned OFF.

int pinButton = 8;

int Relay = 2;

int stateRelay = LOW;

int stateButton;

int previous = LOW;

long time = 0;

long debounce = 500;

int stayON = 5000; //stay on for 5000 ms

void setup() {

pinMode(pinButton, INPUT);

pinMode(Relay, OUTPUT);

}

void loop() {

stateButton = digitalRead(pinButton);

if(stateButton == HIGH && previous == LOW && millis() - time > debounce) {

if(stateRelay == HIGH){

digitalWrite(Relay, LOW);

Arduino Control Relay - Tutorial #5 http://www.electroschematics.com/8975/arduino-control-relay/

3 of 6 9/14/2013 3:29 PM

Page 4: Arduino Control Relay - Tutorial #5

} else {

digitalWrite(Relay, HIGH);

delay(stayON);

digitalWrite(Relay, LOW);

}

time = millis();

}

previous == stateButton;

}

Last members who liked this article

Like 0 StumbleUpon

Tweet 2 0

Find more projects

Turn ON an LED

with a Button and

Read the State of

a Button with

Temperature

Controlled Relay

Blinking an LED

with Arduino –

Get the latest schematics to your e-mail box:

2 Responses to “Control a Relay with Arduino – Tutorial #5”

Richard Knight says: on July 17, 2013 at 11:38 pm

reply

I love simple electronic projects but do not see the point of useing a Complex IC to do the job of a of a 1g 1w switch.

Usually the projects are very good,educational and useful. But there seems to be a trend to use ICs and micro

processors just to find a use for them.

Anyway keep up the good work

Kind regards Richard Knight (retired electrician and lifelong electronics geek).

1.

Popescu Marian says: on July 18, 2013 at 8:42 am

Stepper Control

Tutorialswww.GaliLmc.com

Free Web Tutorials from Galil, the

Leader in Stepper Motor Control.

Arduino Control Relay - Tutorial #5 http://www.electroschematics.com/8975/arduino-control-relay/

4 of 6 9/14/2013 3:29 PM

Page 5: Arduino Control Relay - Tutorial #5

reply

Hi Richard, you are right, but we will get into more complex things in the future, things that usually will require a

lot of components, but by using a microcontroller the circuit and the currenct consumption will be much smaller.

What do you think about this article? Leave a comment!

Name (required)

Mail (will not be published) (required)

Website

Check this box to get notifications via e-mail on new comments.

Captcha *

Type the text displayed above:

ElectroSchematics Partners

ask a question

Arduino Control Relay - Tutorial #5 http://www.electroschematics.com/8975/arduino-control-relay/

5 of 6 9/14/2013 3:29 PM

Page 6: Arduino Control Relay - Tutorial #5

Login

Username or Email

Password

Remember Me

Register

Lost Password

Recent Questions

Hi,i would like to build an electronic 6/8 kv rodent killer and need a circuit diagram with labelled partsby kawabri | 08-09-13

LED short circuit detector circuit for a PSU help.by MartyB79 | 08-09-13

circuit for charging 4.2v battery lead acid to over charginby mukesh gongel | 08-09-13

recently added electronic circuits

Renesas Electronic Scale

Renesas Vacuum Cleaner

Continuity Tester Differentiates Resistance

Become our friend on facebook

Electro Schematics

Like

14,075 people like Electro Schematics.

Facebook social plugin

Policy - Sitemap - Disclaimer - © Copyright 2008 - 2013

Arduino Control Relay - Tutorial #5 http://www.electroschematics.com/8975/arduino-control-relay/

6 of 6 9/14/2013 3:29 PM