domingo, 8 de julio de 2012

Controlando 4 relays con Arduino y Visual Basic 2010

 

image


Para este proyecto se requiere un panel con 4 relays para poder controlar equipos de mayor potencia como motores compresores o simplemente para iluminación de una residencia u un almacén, Las aplicaciones son variadas.
Este control relay board lo pueden conseguirse en ebay.com, no tiene que ser igual al ilustrado, vienen de 2, 4, 8 relays y más. Lo seleccionaran de acuerdo a su proyecto.
Abajo encontraras el sketch de Arduino y el código fuente de VB2010, también dejare el lugar para bajar el archivo (Arduino4ChRelayControl) VB2010.
Espero les funcione bien y recuerden seleccionar el puerto de comunicación USB cuando corran la aplicación en sus PC yo utilizo el Com3, Suerte con el proyecto. Hasta la próxima.

Conecciones electricas:
Pin 13 – canal 1
pin 12 – canal 2
pin 11 – canal 3
pin 10 – canal 4
100_1047
                 
Arduino Sketch

// By: apcexpert.blogspot.com

int RL4 = 13; // the number of the Relay pin
int RL3 = 12;
int RL2 = 11;
int RL1 = 10;


void setup() {
 Serial.begin(9600); // set serial speed

 pinMode(RL4, OUTPUT); // set Relay as output
 digitalWrite(RL4, LOW); //turn off Relay

 pinMode(RL3, OUTPUT);
 digitalWrite(RL3, LOW);

 pinMode(RL2, OUTPUT);
 digitalWrite(RL2, LOW);

 pinMode(RL1, OUTPUT);
 digitalWrite(RL1, LOW);




 }

void loop(){
 while (Serial.available() == 0); // do nothing if nothing sent
 int val = Serial.read() - '0'; // deduct ascii value of '0' to find numeric value of sent number

if (val == 4) { // test for command 1 then turn on Relay
 Serial.println("Relay 4 on");
 digitalWrite(RL4, HIGH); // turn on Relay
 }

if (val == 3) {
 Serial.println("Relay 3 on");
 digitalWrite(RL3, HIGH); 
 }

 if (val == 2) {
 Serial.println("Relay 2 on");
 digitalWrite(RL2, HIGH);
 }

 if (val == 1) {
 Serial.println("Relay 1 on");
 digitalWrite(RL1, HIGH);
 }


 else if (val == 8) // test for command 0 then turn off LED
 {
 Serial.println("Relay 4 OFF");
 digitalWrite(RL4, LOW); // turn off LED
 }


  else if (val == 7)
 {
 Serial.println("Relay 3 OFF");
 digitalWrite(RL3, LOW);
 }

  else if (val == 6)
 {
 Serial.println("Relay 2 OFF");
 digitalWrite(RL2, LOW);
 }

  else if (val == 5)
 {
 Serial.println("Relay 1 OFF");
 digitalWrite(RL1, LOW);
 }




 else // if not one of above command, do nothing
 {
 //val = val;
 }
 Serial.println(val);
 Serial.flush(); // clear serial port
 }



(Arduino4ChRelayControl) Visual Basic 2010


Imports System.IO
Imports System.IO.Ports
Imports System.Threading

Public Class Form1
Shared _continue As Boolean
Shared _serialPort As SerialPort

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.PortName = (“com3”) ‘change com port to match your Arduino portSerialPort1.Close()
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = Parity.None
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Encoding = System.Text.Encoding.Default ‘very important!
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.Open()
SerialPort1.Write(“1”)
SerialPort1.Close()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
SerialPort1.Open()
SerialPort1.Write(“2”)
SerialPort1.Close()
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
SerialPort1.Open()
SerialPort1.Write(“3”)
SerialPort1.Close()
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
SerialPort1.Open()
SerialPort1.Write(“4”)
SerialPort1.Close()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Open()
SerialPort1.Write(“5”)
SerialPort1.Close()
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
SerialPort1.Open()
SerialPort1.Write(“6”)
SerialPort1.Close()
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
SerialPort1.Open()
SerialPort1.Write(“7”)
SerialPort1.Close()
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
SerialPort1.Open()
SerialPort1.Write(“8”)
SerialPort1.Close()
End Sub

End Class

(Arduino4ChRelayControl)

2 comentarios:

Unknown dijo...

Como puedo añadir mas botones porfa?

MEDINA dijo...

ME ENCUENTRO QUE DESPUES DE INSERTAR DOS BOTONES MAS (ON Y OFF ) A LOS ACTUALES (TOTAL 10) RESULTA QUE EL BOTON 9(ON)ENCIENDE EL LED PERO EL BOTON 10(OFF) NO HACE NADA .
¿QUE PUEDE SER? PRETENDO HACER TU IDEA PARA CONTROLAR 16 PINES DE ARDUINO .GRACIAS