Monday, March 24, 2014

Getting Started with the RN42 Bluetooth Module

In this video post tutorial we go over the basics of using the RN42 Bluetooth module. This tiny but capable Bluetooth module makes it easy to add wireless capability to any project or design. Topics covered include:
  • Connecting to and communicating with the RN42 wirelessly 
  • Using the RN42 in command mode to change settings
  • Wireless communication with an Arduino Uno using the RN42


RN42 Tutorial Schematics
Basic Setup with Serial Pins Shorted Together
RN42 Connected to Arduino Uno
Communicating with an Arduino wirelessly using the RN42 Bluetooth module
/*This Arduino Uno sketch was used to communicate with an Arduino Uno wirelessly using a serial terminal and the RN42 Bluetooth module. This code is free and open for anyone to use*/

void setup() {
  //set baud rate to match BT module
  Serial.begin(115200);
}

void loop() {

  String t; //string to hold data from BT module
  while(Serial.available()) { //keep reading bytes while they are still more in the buffer
    t += (char)Serial.read(); //read byte, convert to char, and append it to string
  }

  if(t.length()) { //if string is not empty do the following

    if(t == "Hi Uno\r\n") { Serial.print("Hello Neil\n"); } //say hello
    else if(t == "Meaning of life?\r\n") { //find out the meaning of life
      delay(1000);
      Serial.print("Money. ");
      delay(1000);
      Serial.print("Guns. ");
      delay(1000);
      Serial.print("Hoes.\n");
   }
    else { Serial.print("Syntax Error\n"); } //send this for any other string
   }
   delay(20);
}

Sunday, March 23, 2014

Voltage Level Shifting Tutorial

In this tutorial we look at three methods for shifting or converting a digital logic level from one voltage level to another voltage level. For instance how to convert a 3.3 V serial signal to a 5 V serial signal or vice versa. Voltage level shifting or voltage translation is needed for serial communication between an Arduino Uno (5 V) and an XBee Zigbee module or an RN42 Bluetooth module (3.3 V).


Level Shifting Tutorial Schematics
Level Shifting with a Voltage Divider
Level Shifting with a NPN Transistor
Level Shifting TX Arduino Sketch
/*The code was used for the Arduino Uno and Duo, whichever was acting as the serial communication transmitter in the tutorial at the time. This code is free and open to all to use. */

void setup() {
//Want to use fast baud rate for this example
Serial.begin(115200);
}

void loop() {
// Just transmit same message over and over
Serial.write("forcetronics\n");
delay(10);
}

Level Shifting RX Arduino Sketch
/*The code was used for the Arduino Uno and Duo, whichever was acting as the serial communication reciever in the tutorial at the time. This code is free and open to all to use. */

void setup() {
//Want to use a fast baud rat
Serial.begin(115200);
}

void loop() {
//look for a serial data and print it
while(Serial.available()) {
  char c = (char)Serial.read();
  Serial.print(c);
}

delay(2);
}

Sunday, March 9, 2014

Building a Motion Coded Light

In this project we build a motion coded light. What is a motion coded light? A light that is turned on or off using a certain sequence of hand or body movements (like a secret handshake). To build a motion coded light you need an Arduino Uno, a high power relay, and an infrared switch. Check out the video below to learn more....



Motion Coded Light Schematic

Motion Coded Light Arduino Sketch
//The following Arduino code for the motion coded light is totally open for anybody to use for anything
int tog = LOW; //variable for toggling the light on or off
const int val = 350; //value for tracking if IR switch has been tripped

void setup() {//only setup code is to set digital pin to output
  pinMode(9, OUTPUT); //Using digital pin 9 to control relay
}

void loop() {
  
  if (analogRead(A5) < val) { //look for object in front of sensor (switch is tripped)

    for (int i=0; i<14; i++) { //check for object to move out of sensor range within 420 ms
       delay(30);

       if (analogRead(A5) >= val) { //look for object to move from sensor

         for (int j=0; j<14; j++) { //give the object 420 ms to move back in front of sensor
           delay(30);
          
           if (analogRead(A5) < val) { //look for object in front of sensor
             int activate = HIGH; //variable to track if the light should be turned off or on
             
             for (int c=0; c<100; c++) { //ensure the object stays in front of sensor for 1 second
               delay(10); //delay 10 ms 10o time for 1 sec total
              
               if (analogRead(A5) > val) { //if object is not in front of sensor for 1 sec break out of loop and do not toogle switch
                 activate = LOW; //set variable to not toogle switch
                 break; //break out of loop
               }
             }
             
             if(activate == HIGH) { //if variable is high toogle light switch
               tog = toggleSwitch(tog); //toggle switch value so opposite action is taken next time
               digitalWrite(9,tog); //write value to digital pin to open or close relay
               delay(1500); //delay 1.5 seconds so switch is not unintentionally toogled again
             }
             break;
           }
         }
         break;
       }
    }
  }
  
  delay(100);
}

//function toggles variable that controls switch position
int toggleSwitch(int t) {
  
  if(t==LOW) { return HIGH; }
  else { return LOW; }
}