Archive

Archive for the ‘Arduino’ Category

Ultrasonic Range Sensor

January 27th, 2010 3 comments

Today I recieved a Ultrasonic Range Sensor bought on eBay.

It’s much like the Parallax Ping))), except that it has got a Trig and a Echo pin, instead of the Ping)))’s multipin (Trig and Echo on the same pin)

I quickly made some code in the Arduino IDE and got it running quick…

Just a sidenode from the physics class; as the sound is travelling thru air with a speed of 340 m/s, this can be recalculated to 0.034 cm/microsecond, which is the same as 29.411 microsecond/cm

Arduino with Ultrasonic Sensor

For those who may be interested the code is here:

/* Ultrasonic Sensor

This sketch reads a ultrasonic rangefinder and returns the
distance to the closest object in range. To do this, it sends a pulse
to the sensor to initiate a reading, then listens for a pulse
to return.�  The length of the returning pulse is proportional to
the distance of the object from the sensor.

The circuit:
* +V connection of the Ultrasonic Sensor attached to +5V
* GND connection of the Ultrasonic Sensor attached to ground
* Trig connection of the Ultrasonic Sensor attached to digital pin 2
* Echo connection of the Ultrasonic Sensor attached to digital pin 3

created 25. Januar 2010
by Thomas Jespersen

*/


// this constant won't change.�  It's the pin number
// of the sensor's output:
const int TrigPin = 2;
const int EchoPin = 3;

void setup() {
// initialize serial communication:
Serial.begin(9600);
pinMode(TrigPin, OUTPUT);
pinMode(EchoPin, INPUT);
}

void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm, mm;

// The Ultrasonic Sensor is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(5);
digitalWrite(TrigPin, LOW);

// Read the signal from the Ultrasonic Sensor a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the Trig to the reception of its echo off of an object.
duration = pulseIn(EchoPin, HIGH);

// convert the time into a distance
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
mm = microsecondsToMillimeters(duration);

Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm, ");
Serial.print(mm);
Serial.print("mm");
Serial.println();

delay(100);
}

long microsecondsToInches(long microseconds)
{
// According to Parallax's datasheet for the PING)))
// (another ultrasonic range sensor),
// there are 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second).�  This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.

// 340 m/s
// 34000 cm/s
// 34 cm/ms
// 0.034 cm/micros

// 1/0.034 cm/micros = 29.411 micros/cm

return microseconds / 29.411 / 2;
}

long microsecondsToMillimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter,
// and then we multiply with 10 to get in in millimeters.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.

// 340 m/s
// 34000 cm/s
// 340000 mm/s
// 340 mm/ms
// 0.34 mm/micros

// 1/0.34 mm/micros = 2.9411 micros/mm
return microseconds / 2.9411 / 2;
}

Categories: Arduino Tags: , ,

WiFi SMS Gateway

September 5th, 2009 10 comments

After a long search on the internet, trying to find a cheap SMS Gateway, I got the idea to create one myself.
As I already had a GM862 module laying around, and I’ve got the WiShield WiFi module for the Arduino too, I thought it would be easy – but it wasn’t!

The hardware setup is pretty easy, except the powering for the GM862, as it requires about 3.8V. That was made with a Linear LT1528 3A low dropout voltage regulator.
After alot of testing and measuring, I finally made a working circuit, and afterwards an Eagle Schematic.

WiFi SMS Gateway - Schematic

WiFi SMS Gateway - Schematic

� 

In the following picture you will see the hardware setup, which consists of the Arduino with the WiShield on the top, the GM862 on two breadboards, and alot of wires!

WiFi SMS Gateway - Hardware

WiFi SMS Gateway - Hardware

� 

But the real problems appeared when I started programming! I had alot of troubles with the serial connection between the Arduino and the GM862 module, which apparently wasn’t caused by the hardware setup!

� 
After alot of debugging I figured it out, and a simple SMS sending code was working 🙂
Then after a couple of hours I’ve added the WiFi parsing code which sends an SMS to the requested number, with the requested message, read from the GET parameters!

You can grab the code here, and try it out:

/*
 * WiFi SMS Gateway sketch by Thomas Jespersen - http://elec.tkjweb.dk/blog
 * --- Remeber to change the PIN, if any, in the setup() routine! ---
 */

#include <WiServer.h>

#define WIRELESS_MODE_INFRA	1
#define WIRELESS_MODE_ADHOC	2

// Wireless configuration parameters ----------------------------------------
unsigned char local_ip[] = {192,168,1,111};	// IP address of WiShield
unsigned char gateway_ip[] = {192,168,2,254};	// router or gateway IP address
unsigned char subnet_mask[] = {255,255,255,0};	// subnet mask for the local network
const prog_char ssid[] PROGMEM = {"SSID"};		// max 32 bytes


unsigned char security_type = 0;	// 0 - open; 1 - WEP; 2 - WPA; 3 - WPA2

// WPA/WPA2 passphrase
const prog_char security_passphrase[] PROGMEM = {"Passphrase"};	// max 64 characters

// WEP 128-bit keys
// sample HEX keys
prog_uchar wep_keys[] PROGMEM = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,	// Key 0
				  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 1
				  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,	// Key 2
				  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00	// Key 3
				};

// setup the wireless mode
// infrastructure - connect to AP
// adhoc - connect to another WiFi device
unsigned char wireless_mode = WIRELESS_MODE_INFRA;

unsigned char ssid_len;
unsigned char security_passphrase_len;
// End of wireless configuration parameters ----------------------------------------

//unsigned byte TimeBeforeConnection;

unsigned int lastSMStime = 0;

   char number[7];
   char* message;

void sendSMS() {
  if (millis()/1000 > lastSMStime) {
    Serial.print("AT+CMGF=1\r\n");
    delay(500);
    Serial.print("AT+CMGS=");
    delay(250);
    for (int i=0; i < 8; i++) {
      Serial.print(number[i]);
    }
    //Serial.print(number);
    // Replace with a valid phone number
    Serial.print("\r\n");
    delay(2500);
    Serial.print(message);
    delay(500);

    // End the SMS with a control-z
    Serial.print(0x1A,BYTE);
    lastSMStime = (millis()/1000) + 2;
  }
}

// This is our page serving function that generates web pages
boolean sendMyPage(char* URL) {

    // Check if the requested URL matches "/"
    if (strcmp(URL, "/") == 0) {
        // Use WiServer's print and println functions to write out the page content
        WiServer.print("<html>");
        WiServer.print("<head><title>TKJ's SMS Gateway</title>");
        WiServer.print("<h1>This is TKJ's SMS Gateway</h1><br>");
        WiServer.print("</html>");

        // URL was recognized
        return true;
    }

    // Check if the requested URL starts with "/?"
    if (URL[0] == '/' && URL[1] == '?') {
        for (int i=0; i<8; i++) {
          number[i] = '0';
        }
        message = "";

        for (int i=0; i < 8; i++) {
          number[i] = URL[i+2];
        }
        for (int i=0; i < strlen(URL); i++) {
          message[i] = URL[i+10];
        }

        // Use WiServer's print and println functions to write out the page content
        WiServer.print("<html>");
        WiServer.print("<head><title>TKJ's SMS Gateway</title>");
        WiServer.print("<h1>The SMS is now sent!</h1><br><p>");
        //WiServer.print(message);
        WiServer.print("</p>");
        WiServer.print("</html>");

        sendSMS();

        // URL was recognized
        return true;
    }

    // URL not found
    return false;
}

void setup() {
  // Turn GSM Module ON
  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);
  delay(500);
  digitalWrite(3, LOW);
  delay(500);
  digitalWrite(3, HIGH);
  delay(1000);

  // Type Mobile SIM Pin Code
  Serial.begin(9600);
  // Remember to change the PIN, if any
  Serial.print("AT+CPIN=0000\r\n");

  // Initialize WiServer and have it use the sendMyPage function to serve pages
  WiServer.init(sendMyPage);

  // Enable Serial output and ask WiServer to generate log messages (optional)
  WiServer.enableVerboseMode(false);

}

void loop(){

  // Run WiServer
  WiServer.server_task();
  delay(10);
}


Categories: Arduino Tags: , , , , ,

RFID Modded Safe

June 20th, 2009 3 comments

This project is about my RFID Modded Safe.

I’ve modded an old electronic toy-safe, which is unlocked by typing the 4-char password.

RFID Modded Safe

RFID Modded Safe

I’ve unsoldered the locking-mechanism from the circuitboard, and connected it to the Arduino instead.

Locking-Mechanism Hack

Locking-Mechanism Hack

Then I connected a ID-20 RFID Reader to the Arduino and programmed it to read the tag, and see if it was allowed to enter the safe.

ID-20 RFID Reader

ID-20 RFID Reader

If it was allowed, the Arduino will keep the safe unlocked in 3 seconds!

I’ve also made this video to show how it works:
httphd://www.youtube.com/watch?v=oxuD–7wWE8

You can grab the code here, and try it out:

#include <NewSoftSerial.h>

NewSoftSerial ID12(5,6);
//                RX,TX

// RFID reader ID-12 for Arduino
// Based on code by BARRAGAN
// and code from HC Gilje - http://hcgilje.wordpress.com/resources/rfid_id12_tagreader/
// Modified for Arudino by djmatic
// Modified for ID-12 and checksum by Martijn The - http://www.martijnthe.nl/
//
// Use the drawings from HC Gilje to wire up the ID-12.
// Remark: disconnect the rx serial wire to the ID-12 when uploading the sketch


void setup() {
  Serial.begin(9600);                                 // connect to the serial port
  ID12.begin(9600);
  pinMode(12, OUTPUT);
}

void loop () {
  byte i = 0;
  byte val = 0;
  byte code[6];
  byte checksum = 0;
  byte bytesread = 0;
  byte tempbyte = 0;

  if(ID12.available() > 0) {
    if((val = ID12.read()) == 2) {                  // check for header
      bytesread = 0;
      while (bytesread < 12) {                        // read 10 digit code + 2 digit checksum
        if( ID12.available() > 0) {
          val = ID12.read();
          if((val == 0x0D)||(val == 0x0A)||(val == 0x03)||(val == 0x02)) { // if header or stop bytes before the 10 digit reading
            break;                                    // stop reading
          }

          // Do Ascii/Hex conversion:
          if ((val >= '0') && (val <= '9')) {
            val = val - '0';
          } else if ((val >= 'A') && (val <= 'F')) {
            val = 10 + val - 'A';
          }

          // Every two hex-digits, add byte to code:
          if (bytesread & 1 == 1) {
            // make some space for this hex-digit by
            // shifting the previous hex-digit with 4 bits to the left:
            code[bytesread >> 1] = (val | (tempbyte << 4));

            if (bytesread >> 1 != 5) {                // If we're at the checksum byte,
              checksum ^= code[bytesread >> 1];       // Calculate the checksum... (XOR)
            };
          } else {
            tempbyte = val;                           // Store the first hex digit first...
          };

          bytesread++;                                // ready to read next digit
        }
      }

      // Output to Serial:

      if (bytesread == 12) {                          // if 12 digit read is complete
        Serial.print("5-byte code: ");
        for (i=0; i<5; i++) {
          if (code[i] < 16) Serial.print("0");
          Serial.print(code[i], HEX);
          Serial.print(" ");
        }
        Serial.println();

        Serial.print("Checksum: ");
        Serial.print(code[5], HEX);
        Serial.println(code[5] == checksum ? " -- passed." : " -- error.");
        Serial.println();

        if (code[0] == 0x01 && code[1] == 0x07 && code[2] == 0x7A && code[3] == 0x21 && code[4] == 0xD9 && code[5] == checksum) {
          Serial.println("Case Unlocked by User1");
          digitalWrite(12, HIGH);
          delay(3000);
          Serial.println("Case Locked");
          digitalWrite(12, LOW);
        }
        if (code[0] == 0x01 && code[1] == 0x07 && code[2] == 0x7A && code[3] == 0x1A && code[4] == 0xBA && code[5] == checksum) {
          Serial.println("Case Unlocked by User2");
          digitalWrite(12, HIGH);
          delay(3000);
          Serial.println("Case Locked");
          digitalWrite(12, LOW);
        }
      }

      bytesread = 0;
    }
  }
}

Categories: Arduino Tags: , , , , , , ,

Ethernet TV

After my last success with the Serial TV, I’ve got the idea making it possible for you to the television using a browser.

I’m still using my Serial TV, but now it’s connected to an Arduino instead of the computer. The Arduino has an Ethernet Shield on top, and it’s programmed to make a homepage with a text-field and a submit button!

Here is some pictures of the project:

Ethernet TV - Full

Ethernet TV - Full


Ethernet TV - Arduino

Ethernet TV - Arduino


Ethernet TV - iphone

Ethernet TV - iphone


Ethernet TV - iphone + TV

Ethernet TV - iphone + TV


Ethernet TV - Test Text

Ethernet TV - Test Text

Categories: Arduino, PIC Tags: , , ,

Ethernet DMX

March 25th, 2009 15 comments

Last week I recieved the Arduino Ethernet Shield – http://www.arduino.cc/en/Main/ArduinoEthernetShield
There were all ready complete examples provided, so it was very easy to get started.
Then I thought it would be nice to have a Ethernet DMX Controller, so i could type the IP-Adress of the Ethernet Shield in explorer, and from there control my movinghead (Stairville MV250H). 

Then I found this page showing how to use an MAX485 and the Arduino as a DMX interface – http://iad.projects.zhdk.ch/physicalcomputing/hardware/arduino/dmx-shield-fur-arduino/
I tried it, and it worked 🙂 – And the Arduino could communicate with my movinghead.

Then I combined the Ethernet example and the DMX example, to make a HTML Page where i could click on different buttons to change the DMX values that would be sent to my movinghead.
Try it out yourself… You need a MAX485, follow the link above on how to connect it to the Arduino. Just use pin 3 instead of pin 11, as the Ethernet shield uses Pin 9 to 13!

Read more…

Categories: Arduino Tags: , ,