Posts Tagged ‘arduino’

Small Update to Mirf Library

Posted in Arduino, News on July 13th, 2010 by nisburgh – Be the first to comment

One of my readers asked ( in the forums too ) for some help on reading register values from the Nordic chip, so I put together a quick example and added it to the library. This example reads the RF_SETUP register and prints the value in binary to the Serial output:

/**
 * Pins:
 * Hardware SPI:
 * MISO -> 12
 * MOSI -> 11
 * SCK -> 13
 *
 * Configurable:
 * CE -> 8
 * CSN -> 7
 */

#include 
#include 
#include 

void setup() {
  Serial.begin(9600);
  Serial.println( "Starting wireless..." );

  // Setup
  Mirf.init();
  Mirf.setRADDR((byte *)"clie1");
  Mirf.payload = sizeof(unsigned long);
  Mirf.config();

  // Read and print RF_SETUP
  byte rf_setup = 0;
  Mirf.readRegister( RF_SETUP, &rf_setup, sizeof(rf_setup) );
  Serial.print( "rf_setup = " );
  Serial.println( rf_setup, BIN );

  Serial.println( "Wireless initialized!" );
}

void loop() {}

When I ran this code on my board, it printed:

Starting wireless...
rf_setup = 1111
Wireless initialized!

So that’s 00001111 for the value of RF_SETUP. Breaking it down, high bit to low bit, using the datasheet:

0 – No continuous carrier wave
0 – Reserved – must be 0
0 – RF_DR_LOW is 0
0 – No PLL lock signal
1 – RF_DR_HIGH is 1, and according to chart, 01 = 2 Mbps
1 – High bit RF_PWR is 1
1 – Low bit RF_PWR is 1, according to chart, 11 = 0 dBm output power
1 – Not used/obsolete

You can download the library and bundled examples ( including this one ) from the Software page. Cheers!

More software! This time some fixes to the Mirf library

Posted in Arduino on November 12th, 2009 by nisburgh – 16 Comments

I’ve added another Arduino library to the software page. This one is an updated version of the original Mirf library, used for interfacing with the Nordic Semiconductor RF modules, like the nRF24L01+. I fixed a bug, updated the code here and there, fixed one of the examples and made some other small changes. As I work further with the library, I’ll push changes up here if they’re big. The bug fix was a pretty big one, so I wanted to share it with everyone. Let me know if you have any comments or suggestions!

Wireless is GO!

Posted in Electronics on October 22nd, 2009 by nisburgh – 6 Comments

I got my wireless link online several weeks ago and am just now posting.  Lame, I know, but after it was up I immediately set about working it towards something useful.  I’ll save that for another post; right now I want to detail what was involved in getting the RF link up.
First off, the hardware: two arduinos and two Nordic radios. I used the Arduino USB boards, one RP-SMA board and one chip antenna board.  Why two different RF configurations?  To test range, sensitivity and see what they look like.  Anyhow, the Nordic chips on these boards ( nRF24L01+ ) interface via the Serial Peripheral Interface bus standard, or SPI for short.  SPI is a fairly simple protocol, but the timings can get tricky.  Fortunately, the Arduino has an SPI interface built in, so all we need to do is download and install the SPI library from the Arduino Playground.  Better still, someone sat down and wrote a small interface library for the Nordic chips which encompasses a subset ( the important ones ) of the command set!  Find details and download Mirf.zip here. Update: I updated and fixed some things with the Mirf library and have released it on the site. Check out the “Software” tab at the top, or go here.

Now that you have the necessary software, you need to hook up the hardware.  The bare minimum to get the radios talking is described at the top of the example files from Mirf.zip.  Just open the ping_client and ping_server example files to get started.  Connect Vcc and Ground to your radio, as well as the 5 pins listed at the top of the example files: MISO, MOSI, SCK, CE and CSN.  Set up both Arduinos, and install the client code on one, the server code on the other.  Use a wall wart to power one while you watch console on the other.  You’ll see the round trip times displayed on the client, and packet messages on the server!

One thing I immediately noticed in testing was that the client code would hang if the connection dropped a packet.  That made testing range rather cumbersome, so I looked at the code in ping_client and found this:

while (!Mirf.dataReady()) {
  //Serial.println("Waiting");
}

Hrmm, if he didn’t get the packet back from the server, he’d hang forever, since the server only replies once. Let’s put a timeout in here:

unsigned long time = millis();
while(!Mirf.dataReady()){
  if ( ( millis() - time ) > 3000 ) {
    Serial.println( "Timeout!" );
    break;
  }
  //Serial.println("Waiting");
}
You can't hear it, but they're talking

You can't hear it, but they're talking

Simple! Three second timeout. Now we can do some range tests, and when the connection drops, it will automatically attempt to reestablish after the timeout. You can barely see on the monitor console messages regarding their communication! Perfect!

Well, not quite. I don’t really want to walk around the house with a laptop, arduino, breadboard, nRF module and wires all over the place just to see the output on the console! Let’s interface a serial LCD so we can see real time RTT’s right on the LCD.

/**
 * Example code to run RTT tests between nRF modules and display results on serial LCD
 *
 * Pins:
 * Hardware SPI:
 * MISO -> 12
 * MOSI -> 11
 * SCK -> 13
 *
 * Configurable:
 * CE -> 8
 * CSN -> 7
 *
 * LCD Serial In -> 2
 */

#include 
#include 
#include 
#include 
#include 
#include 

// Scott Edwards BPP serial lcd backpack on pin 2
LCDSerialBackpack lcd( 2, SE_BPP, 9600 );

void setup(){
  Serial.begin(9600);
  lcd.begin();
  lcd.clear();
  lcd.home();
  lcd.blOn();
  lcd.print("Starting wireless");
  /*
   * Setup pins / SPI.
   */

  /* To change CE / CSN Pins:
   *
   * Mirf.csnPin = 9;
   * Mirf.cePin = 7;
   */

  Mirf.init();

  /*
   * Configure reciving address.
   */

  Mirf.setRADDR((byte *)"clie1");

  /*
   * Set the payload length to sizeof(unsigned long) the
   * return type of millis().
   *
   * NB: payload on client and server must be the same.
   */

  Mirf.payload = sizeof(unsigned long);

  /*
   * Write channel and payload config then power up reciver.
   */

  /*
   * To change channel:
   *
   * Mirf.channel = 10;
   *
   * NB: Make sure channel is legal in your area.
   */

  Mirf.config();

  Serial.println("Beginning ... ");
  lcd.cursorTo( 2, 0 );
  lcd.print( "Done" );
  delay(500);
  lcd.clear();
  lcd.home();
  lcd.print( "RTT: " );
}

void loop(){
  lcd.cursorTo( 1, 5 );
  unsigned long time = millis();
  unsigned long time2;
  byte fail = 0;

  Mirf.setTADDR((byte *)"serv1");

  Mirf.send((byte *)&time);

  while(Mirf.isSending()){
  }
  delay(10);
  while(!Mirf.dataReady()){
    if ( ( millis() - time ) > 3000 ) {
      lcd.print( "Timeout!        " );
      fail = 1;
      break;
    }
    //Serial.println("Waiting");
  }

  if ( !fail ) {
    Mirf.getData((byte *) &time);
    time2 = millis();

    Serial.print("Ping: ");
    Serial.println(time2 - time);
    lcd.print(time2 - time);
    lcd.print( " ms        " );
  }

  delay(1000);
}
Wireless goodness!

Wireless goodness!

Here we go. Output to LCD, status messages and we’re mobile with a 9 volt battery! What’s this LCDSerialBackpack.h though? Well, I use serial LCD’s a lot, and so I’m putting together a library to simplify their interface into a standard Print interface with special commands like clear() and blOn(). Makes the code easier to write, and I don’t have to remember the command sequences every time I need an LCD. After I clean the library up some more, I’ll put it up here for anyone that’s interested. If you’re following along at home, just rip out the lcd.* statements and put in the necessary code to display to your LCD.
Anyhow, simple to put together wireless connection. I’m very pleased. Like I said, I immediately set about making this thing useful ( RTT’s are nice and all, but let’s start pushing some real data over the airwaves! ), so expect future posts with more meat!

Fresh Toys!!

Posted in Electronics on October 8th, 2009 by nisburgh – 2 Comments
SFE Box

Deceptively small box

My first order from Sparkfun came in!

Here’s the box, surprisingly not damaged in shipping! Congrats, Fed-Ex!

Joy!

Joy!

More importantly, here’s what was in the box!