Difference between revisions of "Arduino"

From no name for this wiki
Jump to: navigation, search
(Bluetooth 220 V voltage switch)
(connecting relay to your arduino)
Line 30: Line 30:
 
* Connect digital pin 12 to Ch2 or In2 on relay module.
 
* Connect digital pin 12 to Ch2 or In2 on relay module.
 
* e.c.t.
 
* e.c.t.
 +
 +
==== arduino code ===
 +
<source lang="c">
 +
 +
#include <SoftwareSerial.h> 
 +
 +
 +
int relay1 = 13;
 +
int relay2 = 12;
 +
int bluetoothTx = 2; 
 +
int bluetoothRx = 3;
 +
 +
int relay1on = 0;
 +
int relay2on = 0;
 +
 +
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
 +
 +
// the setup routine runs once when you press reset:
 +
void setup() {   
 +
 
 +
 +
  pinMode(relay1, OUTPUT);
 +
  pinMode(relay2, OUTPUT);
 +
 
 +
  Serial.begin(9600);  // Begin the serial monitor§§§§9600bps
 +
 +
  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
 +
}
 +
 +
// the loop routine runs over and over again forever:
 +
void loop() {
 +
  if(bluetooth.available())  // If the bluetooth sent any characters
 +
  {
 +
    byte btInput = bluetooth.read();
 +
    Serial.print((char)btInput); 
 +
   
 +
    if(btInput == 41)
 +
    {
 +
      if(relay1on == 0)
 +
      {
 +
      digitalWrite(relay1, HIGH);
 +
      bluetooth.print("A1 ON");
 +
      relay1on = 1;
 +
     
 +
      }
 +
      else
 +
      {
 +
        digitalWrite(relay1, LOW);
 +
        bluetooth.print("A1 OFF");
 +
        relay1on = 0;
 +
      }
 +
    }
 +
    if(btInput == 42)
 +
    {
 +
      if(relay2on == 0)
 +
      {
 +
      digitalWrite(relay2, HIGH);
 +
      bluetooth.print("A2 ON");
 +
      relay2on = 1;
 +
      }
 +
      else
 +
      {
 +
        digitalWrite(relay2, LOW);
 +
        bluetooth.print("A2 OFF");
 +
        relay2on = 0;
 +
      }
 +
    }
 +
   
 +
  }
 +
  if(Serial.available())  // If stuff was typed in the serial monitor
 +
  {
 +
    // Send any characters the Serial monitor prints to the bluetooth
 +
    bluetooth.print((char)Serial.read());
 +
  }
 +
}
 +
</source>

Revision as of 11:56, 4 May 2014

Bluetooth 220 V voltage switch

Control a household lamp with your smartphone.

Needed:


connecting blue tooth modem to arduino

Important thing is that your modem supports 5v power. Many boards are built for 3v. Only four wires needed:

  • Connect grd on arduino to grd on modem
  • Connect 5v of arduio to vcc on modem
  • Connect arduino digital pin 2 to tx on modem
  • Connect arduino digital pin 3 to rx on modem

Do not use pins on arduino that have a tx and rx printed on them. They are used to communicate with your pc while connected by usb.

connecting relay to your arduino

This is straight forward too:

  • Connect grd on arduino to grd on relay module.
  • Connect 5v of arduino to vcc on relay module.
  • Connect digital pin 13 to Ch1 or In1 on relay module.
  • Connect digital pin 12 to Ch2 or In2 on relay module.
  • e.c.t.

= arduino code

#include <SoftwareSerial.h>  
 

int relay1 = 13;
int relay2 = 12;
int bluetoothTx = 2;  
int bluetoothRx = 3;

int relay1on = 0;
int relay2on = 0;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

// the setup routine runs once when you press reset:
void setup() {     
  

  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  
  Serial.begin(9600);  // Begin the serial monitor§§§§9600bps

  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
}

// the loop routine runs over and over again forever:
void loop() {
  if(bluetooth.available())  // If the bluetooth sent any characters
  {
    byte btInput = bluetooth.read();
    Serial.print((char)btInput);  
    
    if(btInput == 41)
    {
      if(relay1on == 0)
      {
       digitalWrite(relay1, HIGH);
       bluetooth.print("A1 ON");
       relay1on = 1;
       
      }
      else
      {
        digitalWrite(relay1, LOW);
         bluetooth.print("A1 OFF");
         relay1on = 0;
      }
    }
    if(btInput == 42)
    {
      if(relay2on == 0)
      {
       digitalWrite(relay2, HIGH);
       bluetooth.print("A2 ON");
       relay2on = 1;
      }
      else
      {
        digitalWrite(relay2, LOW);
         bluetooth.print("A2 OFF");
         relay2on = 0;
      }
    }
    
  }
  if(Serial.available())  // If stuff was typed in the serial monitor
  {
    // Send any characters the Serial monitor prints to the bluetooth
    bluetooth.print((char)Serial.read());
  }
}