Archive for the ‘Arduino’ Category

Circuit Gear, Arduino and Counting Pulses

December 28, 2008

 

Square Wave Generated by Circuit Gear

Square Wave Generated by Circuit Gear

Arduino has a convenient function called pulseIn that you can use to count the HIGH or the LOW of a pulse. To test this, I created a square wave using the excellent Circuit Gear Waveform Generator.

I uploaded this program and connected the output of the waveform generator to the board (connect the Red wire to the Arduino pin 3, and the black to the Arduino digital ground). The program started printing the count of the HIGH pulses. As I adjusted the frequency of the square ware, the print out was getting faster or slower depending on the frequency of the wave. But as I started changing the amplitude of the square wave, I noticed than when the voltage difference between the LOW and HIGH of the square wave was less than 4 volts, the pulseIn function could no longer read the HIGHs. The documentation for the HIGH confirmed that the difference between the LOW and HIGH must be at least greater than 3 volts. In my case, anything less than 4 volts just didn’t work.

To show a waveform on the Circuit Gear, connect the output of the generator (leftmost connector) to one of the oscilloscope inputs (the two on the right). You’ll need something that can connect to BNC connectors, either alligator clip leads or adaptors or scope probes.

In other words, there is no internal connection between the output of the generator and the input of the oscilloscope, you have to add that.

(If you had another oscilloscope, you could also connect the output of the generator to that oscilloscope and view the waveform.)

Here is the pulse counter program for the Arduino:


int pulsePin = 3;
unsigned long counter = 0;
unsigned long duration = 0;
unsigned long timeout = 1000000; // in microseconds

void setup() {
  pinMode(pulsePin, INPUT);
  // enable the 20K pull-up resistor to steer
  // the input pin to a HIGH reading.
  digitalWrite(pulsePin, HIGH);
  Serial.begin(9600);
  Serial.println("Here we go again");
}

void loop() {
  duration = pulseIn(pulsePin, HIGH, timeout);
  if (duration == 0) {
    Serial.print("Pulse started before the timeout.");
    Serial.println("");
  } else {
    counter++;
    Serial.print(counter);
    Serial.print(", ");
    Serial.print(duration);
    Serial.println("");
  }
}

How to control GM862 from Arduino

December 20, 2008
Arduino to GM862 pin connection

Arduino to GM862 pin connection

It took some effort to figure out how to connect the GM862 GSM module to a UART (in my case the Arduino USB evaluation board, so I decided to document the steps here.

I also have the GM862 USB Evaluation board, if you plan to use this board and control the GM862 from Arduino, e.g., to programmatically send an SMS message, then you need to be careful how you power the GM862 USB board. If you power it via a battery or external power source (but not USB), then you’re fine. Otherwise, you must remove the jumper that disconnects the TX and RX pins from the GM862 USB FT232. To remove this jumper, you must remove the solder that is on the pins marked TX-I and RX-O. This Sparkfun.com board is not designed to accept a two pin header and a plastic jumper to allow you to easily switch between USB control and external control. So to get going, don’t use the GM862 USB to power the GM862, whilst it is being externally controlled through your Arduino board.

For a minimum implementation, only the TXD and RXD lines needs to be connected, the other signals of the GM862 modem serial port (DCD/pin 36, DTR/pin 43, DSR/pin 33, RTS/pin 45, CTS/pin 29, and RI/pin 30) can be left open provided a software flow control is implemented (which is what we are trying to do).

You’ll need to disconnect the RX pin 0 on the Arduino board each time you upload your program. If you don’t, upload will fail with the “avrdude: stk500_recv(): programmer is not responding” error message. After you’ve uploaded the program you can reconnect pin 0. Pin 1 can remain connected at all times, so there is no need to disconnect pin 1.

It helps to have a LED attached to the status indicator LED (pin 39 of GM862), but this is optional and not required for you to control the GM862 from Arduino. Pin 39 is an Open Collector output where we can directly connect a LED to show network and call status information (fast blink means net search, not registered or turning off, slow blink means registered full service, always on means a call is active). I connected a LED with a pull-up resistor.

After connecting the Arduino RX pin (pin 0) to GM862 RX-O pin (pin 37), Arduino TX pin (pin 1) to GM862 TX-I pin (pin 20), and Arduino Ground to GM862 Ground, you are ready to take control of GM862 right from Arduino (click on the thumbnail for detailed schematics). To test this set up, send a SMS message. Here is the code to do just that:

#include <SoftwareSerial.h>

int rxPin = 0;
int txPin = 1;

// set up a new serial port
SoftwareSerial serial=SoftwareSerial(rxPin,txPin);

void setup()  {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);

  // set the data rate for the SoftwareSerial port
  serial.begin(9600);

  // Set SMS to text mode. Note it is critical 
  // to use \r\n to end each line
  // The delays are also critical, without them, 
  // you may lose some of the
  // characters of your message

  serial.print("AT+CMGF=1\r\n");
  delay(300);
  serial.print("AT+CMGS=");
  delay(300);
  // Replace with a valid phone number
  serial.print("+14081234567\r\n");
  delay(300);
  serial.print("Hello from Arduino.");
  delay(300);

  // End the SMS with a control-z
  serial.print(0x1A,BYTE);
}

void loop() {
}

Here is a function that takes a char array and use that as the content of the SMS. Not sure if a delay after each print statement is really needed or not.


#define PHONE_NUMBER  "+14081234567\r\n" 
#define PRINTDELAY 500

void sendSms(char msg[]) {
  modem.print("AT+CMGF=1\r\n");
  delay(PRINTDELAY);
  modem.print("AT+CMGS=");
  modem.print(PHONE_NUMBER);
  delay(PRINTDELAY);
  modem.print(msg);
  delay(PRINTDELAY);
  modem.print(",");
  delay(PRINTDELAY);
  modem.print(millis());
  delay(PRINTDELAY);
  // End the SMS with a control-z
  modem.print(0x1A,BYTE);
  delay(PRINTDELAY);
}

 


Follow

Get every new post delivered to your Inbox.