Tuesday, June 3, 2014

Arduino square wave pulse generator using the tone() library and serial port





Code:

/**
* Square wave pulse  generator
* How to use it: Send desired frequency in Hz using serial port Baud = 9600 8,N,1
* Note: CR or LF must be send as a line termination.
*
* Elimelec June-3th-2014
*/

unsigned int freq = 0; //frequency

int outPin = 13; //output pin


void setup(){

  Serial.begin(9600);//setup serial communication

}

void loop(){
  
  while(Serial.available() > 0){
      
      char tmpChar = Serial.read(); //read incoming character from serial port
  
     if(isDigit(tmpChar)){ //check if the in character it's a number
      
       freq =  (freq  * 10 ) + tmpChar - '0'; // convert from ascii code to it's corresponding number; '0' == DEC 48
     }       
     
     if(tmpChar == '\n' || tmpChar == '\r'){ //if CR or LF was received 
      
       if(freq > 0){
         Serial.print(freq);
         Serial.println(" Hz Set O.K");
         tone(outPin, freq); //set current frequency
       }
      
       freq = 0; // clear freq
     }
     
  }
   
}

Thursday, October 24, 2013



GPS Tracker Arduino


You will need:

Hardware:

1 Arduino board
1 SIM900 GSM Shield
1 GPS Receiver (Optional)
1 NPN transistor
2 220 Ohm resistors
RS232 to TTL or USB to TTL  (Optional)
Full Internet Access SIM Card


Software:

Arduino sketch
GPS Simulator
GPS Simulator Route file
GPS-Trace Account (FREE)
Bray's terminal



Documentation:

GSM Module from geeetech

SIM900



Step by Step guide


Step 1

Connect your arduino board as follow:




Note: Use the transistor only if you don't have an RS232 to TTL or USB to TTL cable.


Step 2

Modify the following lines of the arduino code:

line 128: char apn[] = "put_your_APN_here";  

                                                              
line 460:  mySerial.print("+RESP:GTFRI,02010B,your_SIM900_IMEI_15digits,GL200,0,0,1,1,");


Step 3

Configure your GSM modem to a baud rate of 9600 by sending the command AT+IPR=9600 using the bray's terminal, don't forget to check the option +CR in the send tab.

Note: If you don't have an RS232 to TTL or USB to TTL cable, load the following code into your arduino board, in order to change the baud rate.

//Change baudrate from 115200 to 9600

#include <SoftwareSerial.h>

SoftwareSerial mySerial (7,8); //RX, TX  for GSM/GPRS Modem



void setup (){

mySerial.begin(115200); // the baud rate your modem it's currently configured.

}

void loop(){

mySerial.println("AT+IPR=9600");
delay(3000);

}



Step 4

 Load the arduino sketch into your board.

Step 5

>Install Avangardo GPS Simulator.

>Configure the Serial port baud rate to 4800:

Go to "settings/output settings/serial port"



>Load the "Route file" and press "start".






Note:
> Zoom in and Out with the mouse wheel.
>To navigate through the map, press and hold the right mouse button.



Step 6

Open the Arduino Serial Monitor (9600) to verify that the system it's working correctly. If everything it's o.k, you will see the NMEA output printed every 15 seconds, after that time, the communication status of the modem will be printed instead.

If you don't see any data string, double check the connection and verify the communication with the modem and/or GPS simulator.


Step 7


>Go to GPS-Trace and create an account (Free).

>Login  to your account and click the wrench icon.

>Configure your device IMEI, Name and  GPS vendor.

>Select google map (Optional)

Step 8

Restart the track simulation of the GPS simulator and restart your modem. Once the track simulation has ended, you can generate a trajectory with GPS-Trace.



O.K, everything worked flawlessly for me, but the GPS Simulator software time trial has expired, what should I do?


If your are working under  Ubuntu (GNU/Linux ):

>Close the GPS Simulator software
>Go to your home folder.
>Press "Ctrl+l" (L) to display the navigation bar (Ubuntu).
>Open the hidden folder ".wine".
>Delete the files "system.reg" and "user.reg".
>Go to "Drive_c/program Files" and delete the folder "Avangardo".
>Reinstall the GPS Simulator software.

Sorry, but I don't use GNU/Linux. I'm a Windows user.

For windows users, just purchase a license (85.00 USD).

Jeje, I'm just kidding. Please follow this article.






Thanks for reading.

Elimeléc




































Sunday, August 25, 2013

Remote Door Open by SMS using Arduino



GSM Module used:

SIM900
Wiki
Testing the module


Video:


Code:
// this code was tested using the modem SIM900

/*

this code turn "On" and "Off" an LED by SMS.

Commands:

$ON$
$OFF$

Note: the commands are case sensitive.

*/


#include <SoftwareSerial.h>

SoftwareSerial mySerial(7, 8); // RX, TX

byte led = 13;   // use "byte" instead of "int" to save memory.
byte led_on = 0;
byte led_off = 0;
byte dollar_count = 0;

long last_millis = 0;
const int delay_time = 5000;

const int buff_size = 10;
char buffer[buff_size];

void setup()  
{
  pinMode(led, OUTPUT);
  mySerial.begin(9600);
  Serial.begin(9600);
 
   mySerial.println("AT\r\n"); // check communication with the modem.
   delay(500);
   mySerial.println("AT\r\n");
   
   //delete all read SMS
   
   for(int x=1; x<=20; x++){
   mySerial.print("AT+CMGD=");
   mySerial.print(x);
   mySerial.print(",");
   mySerial.print("3");
   mySerial.print("\r\n");  
   delay(1000);
   }
   
  
 
}

void loop(){

     
     if ((millis() - last_millis) > delay_time ){
       mySerial.print("AT+cmgl=\"REC UNREAD\"\r\n");
       last_millis = millis();
     }
  

  if (mySerial.available() > 0){

    char inchar = mySerial.read(); // print in data to  software serial port for debug perpose.
    
    Serial.print(inchar);

    if(inchar == '\r' || inchar == '\n'){
      dollar_count = 0;
      led_on = 0;
      led_off = 0;
    }

    if(inchar == '$'){
      dollar_count++; 
     } 
    

      switch (dollar_count){
      case 1:
      
      if(inchar == 'O' || inchar == 'N'){
        led_on++;
      }
      
      if(inchar == 'O' || inchar == 'F'){
        led_off++;
      }
      
      if(led_on == 2){
        digitalWrite(led, HIGH);
      }
      
      if(led_off == 3){
        digitalWrite(led, LOW);
      }
      
      break;
        
      }  


  }


}




                                                                                           By: Elimeléc 




















Friday, July 19, 2013

NMEA checksum calculator



Understanding NMEA Checksum

 
The checksum (CS) is generated by X-OR’ing all the data characters together in sequence, starting with the first ‘data’ character and XOR it with the next one. Then take that result and XOR it with the next, and so on.
Note: The ‘$’ and ‘*’ characters are excluded from calculation.
The example below show how to calculate the CS for the word “test”.


Note: You can use the windows or ubuntu calculator in advance mode to perform this operation.
Step1:
t XOR e


 
Result1 = 0010001 = 11 HEX

Step2:
Result1 XOR s

Result2 = 1100010 = 62 HEX
 
Step3:
Result2 XOR t


 
CS = 0010110 = 16 HEX
 External links

Enough talking, now it's fun time!

Arduino code

// Sample sentences to XOR
//$test*16
//$GPRMC,023405.00,A,1827.23072,N,06958.07877,W,1.631,33.83,230613,,,A*42

const byte buff_size = 80; // buffer size must be a constant variable
char buffer[buff_size];
byte index = 0;   // declare all variables that will hold numbers less than '255' as 'byte' data type, because they require only '1-byte' of memory ('int' uses 2-bytes).
byte start_with = 0;
byte end_with = 0;
byte CRC = 0;
boolean data_end = false; // Here we will keep track of EOT (End Of Transmission).

void setup(){

  Serial.begin(9600);
  Serial.println("Serial communication initialized"); // Print to serial port so we know its working.

}


void loop(){

  while (Serial.available() > 0){
    char inchar = Serial.read();
    buffer[index]  = inchar;

    if( inchar == '$'){
      start_with = index;
    }

    if(inchar == '*'){
      end_with = index;
    }

    index++;

    if(inchar == '\n' || inchar == '\r'){ // if 'new line' or 'carriage return' is received then EOT.
      index = 0;
      data_end = true;
    }
  } 


  if (data_end == true){
    for (byte x = start_with+1; x<end_with; x++){ // XOR every character in between '$' and '*'
      CRC = CRC ^ buffer[x] ;
    }
  }

  if(CRC > 0){
    Serial.println(CRC,HEX); // print calculated CS in HEX format.
    CRC = 0; // reset CRC variable
    data_end = false; // Reset EOF so we can process more incoming data.
  }


}

//Elimeléc López - July-19th-2013







Friday, June 14, 2013

Measure RPMs Arduino



Video




Schematic





The Fan



Code

Download RPM.ino
// read RPM

volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile
int rpm = 0;
unsigned long lastmillis = 0;

void setup(){
 Serial.begin(9600); 
 attachInterrupt(0, rpm_fan, FALLING);//interrupt cero (0) is on pin two(2).
}

void loop(){
 
 if (millis() - lastmillis == 1000){  /*Uptade every one second, this will be equal to reading frecuency (Hz).*/
 
 detachInterrupt(0);    //Disable interrupt when calculating
 
 
 rpm = rpmcount * 60;  /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
 
 Serial.print("RPM =\t"); //print the word "RPM" and tab.
 Serial.print(rpm); // print the rpm value.
 Serial.print("\t Hz=\t"); //print the word "Hz".
 Serial.println(rpmcount); /*print revolutions per second or Hz. And print new line or enter.*/
 
 rpmcount = 0; // Restart the RPM counter
 lastmillis = millis(); // Uptade lasmillis
 attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
  }
}


void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
  rpmcount++;
}


// Elimelec Lopez - April 25th 2013

Code v2 (Calculate Average)
Download RPM_V2.ino

// read RPM and calculate average every then readings.
const int numreadings = 10;
int readings[numreadings];
unsigned long average = 0;
int index = 0;
unsigned long total; 

volatile int rpmcount = 0;//see http://arduino.cc/en/Reference/Volatile 
unsigned long rpm = 0;
unsigned long lastmillis = 0;

void setup(){
 Serial.begin(9600); 
 attachInterrupt(0, rpm_fan, FALLING);
}

void loop(){
 
  
 if (millis() - lastmillis >= 1000){  /*Uptade every one second, this will be equal to reading frecuency (Hz).*/
 
 detachInterrupt(0);    //Disable interrupt when calculating
 total = 0;  
 readings[index] = rpmcount * 60;  /* Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use rpmcount * 30.*/
 
 for (int x=0; x<=9; x++){
   total = total + readings[x];
 }
 
 average = total / numreadings;
 rpm = average;
 
 rpmcount = 0; // Restart the RPM counter
 index++;
 if(index >= numreadings){
  index=0; 
 } 
 
 
if (millis() > 11000){  // wait for RPMs average to get stable

 Serial.print(" RPM = ");
 Serial.println(rpm);
}
 
 lastmillis = millis(); // Uptade lasmillis
  attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
  }
}


void rpm_fan(){ /* this code will be executed every time the interrupt 0 (pin2) gets low.*/
  rpmcount++;
}








Saturday, June 8, 2013

Door Open with Dallas iButton Arduino

 
 Requirements
 
 
 Video

 
Schematic 

 
 
 
 
 Breadboard Connection

 


 Code

 
 #include <OneWire.h>

OneWire ibutton (2); // I button connected on PIN 2.

byte ibuttonid[10] = {1,144,74,165,21,0,0,143}; // Ibutton ID must be typed in Decimal in the following order: Family code (01 for DS1990); numbers from R. to L.; CRC. 
byte buffer[10]; //array to store the readed Ibutton ID.

boolean result;  // this variable will hold the compare result

int doorpin = 13; // the output pin to activate the door.

void setup(){
    Serial.begin(9600); 
    pinMode(doorpin,OUTPUT);
   
}

void loop(){
   
 if (!ibutton.search (buffer)){//read attached ibutton and asign value to buffer
    ibutton.reset_search();
    delay(200);
    return;
 }
  
  for (int x = 0; x<8; x++){  
    Serial.print(buffer[x],HEX); //print the buffer content in LSB. For MSB: for (int x = 8; x>0; x--) 
    Serial.print(" "); // print a space
   }
   Serial.println("\n"); // print new line
   
   // compare the ibutton id
   
  result = true; // set variable equal to one.
  for (int x=0; x<10; x++){ 
  int compare1 = ibuttonid[x];// asign each index of arrays to test, one by one and compare
  int compare2 = buffer[x];
  
  
  if(compare1 != compare2){ // if any index comparison is not equal, then the arrays are not equal and result will be 0.
   result = false; 
  }
  
  }
  if(result == true){ // if the arrays are equal, do something.
   Serial.println("Door open for 5 seconds."); 
   digitalWrite(doorpin,HIGH); // Turn on LED on pin 13 (build-in on the Arduino board)
   delay(5000); // wait five seconds. (1 second = 1000 milliseconds)
   Serial.println("Door closed");
   digitalWrite(doorpin,LOW); // turn off LED.
 }
   
    //set buffer back to cero.
    
    for (int x=0; x<10; x++){
     buffer[x] = 0 ;
    }                
}



/* by Elimeléc 
  Jun-07-2013
*/

Friday, June 7, 2013

Read Dallas iButton Arduino

Requirements:
 
-Arduino board
-OneWire Library (unzip and copy to arduino-1.0.3/libraries)
-Maxim Ibutton DS1990
-Maxim Touch Probe Read
 
 
 Schematic
 
 
 Breadboard Connection

 
Code: 
  
 #include <OneWire.h>

OneWire ibutton (2); // I button connected on PIN 2.

byte buffer[20]; //array to store the Ibutton ID.

void setup(){
 Serial.begin(9600); 
   
}

void loop(){
   
 if (!ibutton.search (buffer)){//read attached ibutton and asign value to buffer
    ibutton.reset_search();
    delay(200);
    return;
 }
  
  for (int x = 0; x<8; x++){  
    Serial.print(buffer[x],HEX); //print the buffer content in LSB. For MSB: for (int x = 8; x>0; x--) 
     Serial.print(" "); // print a space
   }
   Serial.println("\n"); // print new line
   
   //crc compute//
   byte crc;
   crc = ibutton.crc8(buffer, 7);
   Serial.println(crc,HEX);
 
     
} 
 
/*by Elimeléc
 Jun-07-2013
*/