Thursday, August 14, 2014

Arduino UNO nRF Adapter

nRF24L01 adapters


This blog entry is about my process in making an Arduino UNO nRF24L01 Adapter from start to the current version... ( If you would like to order a pair of the nRF Adapter, please click on nRF Adapter for Sale. )


Do you face the problems of messy jumpers cables like below when using nRF24L01+ with an Arduino UNO on a breadboard ??

nRF24L01 on breadboard
Or need to DIY your own adapter to sit in between the breadboard groove like the picture below ?

DIY nRF for breadboard

nRf24L01 on Arduino UNO

nRF24L01 with external antenna

With this problem, I started my journey to solve this problem with a nRF Adapter... below are pictures of the evolution of the adapter.

Perfboard first version

The orange and white wires fly across the UNO for the 3.3V power needed by the nRF24L01 radio.
nRF adpater with bottom wiring

nRF adapter with top wiring

PCB Version with 3.3V LDO ( PCB designed by ZXLee )

nRF20L01 adapter DIY PCB 1

nRF24L01 adapter DIY PCB 1

nRF24L01 adapter DIY PCB 1


PCB Version 2 with 2 buttons ( PCB designed by Vintronics )


nRF24L01 adapter DIY PCB 2 bottom

nRF24L01 adapter DIY PCB 2 top

We took the above design, dropped the 2 buttons , fine tuned it and made the final version for factory PCB. This is a joint collaboration project between Arduino for Beginners blog and Vintronics ..

Final factory made PCB version ( designed by Vintronics & myself )

This version have a AMS1117-3.3 at the bottom of the PCB.
nRF24L01 adapter PCB front

nRF24L01 adapter PCB back

Final version with headers soldered on PCB

nRF24L01 adapter PCB front

nRF24L01 adapter PCB back


nRF24L01 Adapter with radio

nRF24L01 adapter with radio

nRF24L01 adapter PCB with radio

nRF Adapter on Arduino UNO

The nRF adapter uses pin 8 for CE, pin 9 for CSN and pin 10 for Vcc (5V to 3.3V with the AMS1117-3.3V VR )

nRF Adapter on Arduino UNO

nRF Adapter on Arduino UNO
All the nRF adapters together ..

All nRF24L01 adapters

If you would like to order a pair of the nRF Adapter, please click on nRF Adapter for Sale.

Summary Links :-

- My RF24 repo fork with support of the nRF Adapter :  https://github.com/stanleyseow/RF24
- Test your nRF24L01 transfer speed https://github.com/stanleyseow/RF24/tree/master/examples/Transfer
- More info on nRF24L01 http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo



Monday, May 12, 2014

TinySafeBoot on attiny84

The TinySafeBoot, this is a small bootloader that will take up 550 bytes so you are left with around 7450 bytes of sketch left. Let me explain a bit of details and instructions as the TinySafeBoot site were not catering to Arduino users in terms of step by steps instructions for beginners.

The reason to install this TinySafeBoot is pretty obvious, you can directly program the attiny84 similar like Arduino UNO using on Serial Tx/Rx pins instead of using the UsbTinyISP. I am connecting a FTDI USB Serial module for both programming and debugging functions using <TinyDebugSerial.h>.


Attiny84 with FTDI

The above image is my prototype attiny84 with FTDI module Serial Debug / Monitor to pin PB0(tx) and PB1(rx).

The steps are as follows :-

1. Download TinySafeBoot from http://jtxp.org/tech/tinysafeboot_en.htm

2. Generate the bootloader hex for attiny84 :-

tsb tn84 b1b0  ( b1 = pin PB1/rx b0 = pin PB0/tx )

3. Burn the above generated hex bootloader to the attiny84

avrdude -c usbtiny -p t84 -U flash:w:<tsb_tn84_b1b0_20140414.hex>

4. Set the fuse bits on attiny :

avrdude -c usbtiny -p t84 -U efuse:w:0xFE:m ( Turn on SELFPRGEN bits )

5. To get the status of the bootloader, RESET the attiny84, wait for 2-3 secs and execute the commands :-

tsb COM5 i  ( get current status from the bootloader )

or

6. To burn a hex to the attiny84, RESET the attiny84, wait for 2-3 secs and execute the commands :-

tsb COM5 fw blink84.hex

( You can compile the hex under Arduino UNO by choosing the board attiny84 @ 8Mhz and copy the hex files to the tsb folder. To see where the hex is compiled and located, Under Arduino IDE Preferences, Click on "Show verbose output during [x] compilation" )

Final product on Tiny nRF V1.0

Tiny nRF V1.0


Summary Links :-

- TinySafeBoot
- Attiny84 Fuse calculator http://eleccelerator.com/fusecalc/fusecalc.php?chip=attiny84
- Arduino Tiny codes https://code.google.com/p/arduino-tiny/
- Arduino Forum on attiny84/85 http://forum.arduino.cc/index.php?topic=115822.msg1698859#msg1698859


Sunday, November 24, 2013

Stellaris Launchpad with nRF24L01+ communicating with Arduino UNO

Firstly I would like to thanks Premier Farnell (Element14) for sending me a sample Texas Instruments's Stellaris Launchpad. This development board platform from Texas Instruments is running on ARM Cortex-M4F based microcontrollers. The diagram below is directly taken from the datasheet showing all the pins, functions and other connectors.

Stellaris Launchpad
As I have lots and lots of female-female jumper cables, I kinda like the male headers on the Stellaris Launchpad. As this is not a review on the Stellaris Launchpad, I shall skip most of the features of the board and jump straight into the software needed to run this board.

Energia


Similar to Arduino, Stellaris have Energia, an open source multiple platform software development environment. You can refer to Stellaris LaunchPad Guide for all the pins and functions.

After installing the Energia development environment, it looks very similar like Arduino except it is red in colour like the Stellaris board.

So the first thing to do is to blink those three built-in RGB LED on the board itself and it looks very nice and useful to have RGB LED on the board itself. The second project that came into my mind is to get the Nordic nRF24L01+ radios working with this launchpad.

With some googling, I found the spirilis had written an Energia nRF24L01 library on the MSP430 (43oh.com) forum. There is also a github repo from spirilis that I downloaded and install the Enrf24 library similar to installing Arduino libraries. This is great, no steep learning curve.

nRF24L01 Stellaris Launchpad


While loading the Enrf24 Tx example, I notice it was written for MSP430 launchad pins instead of Stellaris launchpad pins, so the pin name needed to be changed to matched the Stellaris pin names.

The changes made as follows :-

// PE_1 = CE, PE_2 = CSN, PE_3 = IRQ
// SPI pins : SCK = PB_4 , MOSI = PB_7, MISO = PB_6
Enrf24 radio(PE_1, PE_2, PE_3); 

Refer to the StellaPad https://github.com/energia/Energia/wiki/Hardware#stellarpad-ek-lm4f120xl

Just blindly hook up the nRF24L01 to the Stellaris Launchpad according to the pinout above and you are all set.


While hooking up the 3.3V power to the nRF24L01+ radio ( that requires 3.3V ) I accidentally hook up the jumper cables to the VBUS pins as they are just next to each other. The VBUS is the USB power measured at 4.4V on my multimeter and the left pins besides it is the actual regulated 3.3V power pins. Luckily, the higher voltage did not blew up the nRF radio.

After this minor incident, I got the nRF24L01 working and transmitting a ON/OFF payload to whichever devices that is listening on those configured address.

As I have experience is getting different nRF libraries working with each other, the few possible places to look are the addressing schemes ( either 3 or 5 bytes ), CRC is either off / on (8bit/16bit), does it implement dynamic or static payload and matching all the speed and channels.

After much tweaking on the Arduino UNO side, I still could not get Arduino UNO side to receive the payload so I did what everyone else would do, read the codes and ask the author of this library.

Within less than a few hours, the author spirilis replied me and after changing the CRC settings, the Arduino UNO running the RF24 library is talking to the Stellaris Launchpad. The Tx demo is working and I tried the Rx demo and both works flawlessly. Below are the settings needed to get them working together...


Stellaris Launchpad transmitter side :-
const uint8_t txaddr[] = { 0xDE, 0xDE, 0xDE, 0xDE, 0xE3 };

void setup() {
....
  radio.begin(1000000);  // 1Mbps, max TX power
  radio.setChannel(88); // Channel 88
  radio.setCRC(1,1);    // Enable CRC, 16-bit
....
}

Arduino UNO receiver side :-

const uint64_t pipes[6] = { 0xDEDEDEDEE7LL, 0xDEDEDEDEE9LL, 0xF0F0F0F0E2LL, 0xF0F0F0F0E3LL, 0xF0F0F0F0E4LL, 0xF0F0F0F0E5LL };

void setup() {
....
  radio.setDataRate(RF24_1MBPS);
  radio.setPALevel(RF24_PA_MAX);
  radio.setChannel(88);
  radio.enableDynamicPayloads();
  radio.setCRCLength(RF24_CRC_16);
....
}


Summary Links :-

Premier Farnell (Element14)
Stellaris Launchpad
Energia Development Environment
- Spirilis Energia nRF24L01 library
Forum link


Wednesday, August 14, 2013

Getting nRF24L01 working with attiny84

attiny84


After getting the attiny85 (8 pins) working with nRF24L01, I find that while this chip is nice and small, the lack of extra pins is a real hassle when I need to add any sensors/actuators to the MCU.

When I received my attiny84 ICs from Element14, now the fun begins...

attiny84 pinout

As usual, I hook up the attiny84 and test a blink sketch to ensure the attiny84 is working correctly. The first thing I encounter is that the blink is very slow, about 8 to 10 secs slower and I was wondering why. By the way, I'm using this Arduino tiny-core https://code.google.com/p/arduino-tiny/ as there are other tiny-cores available on the net with different settings.

After posting my problems on Arduino forum, I found out I need to do a Tools->Burn Bootloader to set the correct speed and fuses on the attiny84. After the "Burn Bootloader", the attiny84 was operating correctly at 8Mhz without an external crystal.

attiny84 
The above pictures are my test setup of the attiny84 on a breadboard with breadboard power supply running on 3.3V, nRF24L01 on an UNO adapter, USBtinyISP and logic analyzer and mini hooks.

The "fun" starts when trying to get the SPI pinout for attiny84 for running the nRF24L01. Since it uses the same tiny core was the attiny85 and I got the attiny85 working, it should not be too difficult, I guess.

After hooking up the attiny84 according to the above image and flashed the hex into the attiny84, I did not get correct settings on the four register I was monitoring on the serial/debug monitor. For troubleshooting, I have enabled "TinyDebugSerial" to read the four register, namely RF_CH, RF_SETUP, TX_ADDR and RX_ADDR.

I always needed to verify these settings and proper communications to the nRF24L01 as I used many different data rate, channel/frequencies and TX/RX address on all these nRF24L01 radios all the time.

  Mirf.readRegister(RF_CH, &rf_ch,sizeof(rf_ch));
  Mirf.readRegister(RF_SETUP, &rf_setup, sizeof(rf_setup));
  Mirf.readRegister(TX_ADDR, tx_addr, sizeof(tx_addr));
  Mirf.readRegister(RX_ADDR_P1, rx_addr, sizeof(rx_addr));

I also hook up a Logic Analyzer to the SPI pins to "see" what is going on during those SPI transfers. A proper SPI transfer would look something like below, with Enable/Slave Select (SS) pin held low during active SPI with Clock (SCK) supplying the clockrate for the MOSI/MISO.

SPI transfers

The issue seems to be SPI related as I do not get SPI patterns similar to the above, I immediately looked at SPI85 library that was downloaded from Arduino forum and it was working fine on the attiny85 on my earlier blog entry.

A bit about the SPI for the attiny, as the attiny does not have dedicated pins for SPI, it uses Universal Serial Interface (USI) for both SPI or I2C. See http://playground.arduino.cc/Code/USI-SPI for details on the USI-SPI on Arduino playground.

By viewing this link on Arduino Playground, it gives *hint* NOT to use the MOSI/MISO pins for attiny as they are ONLY used for In-Circuit Serial Programming (ICSP) only.

By looking at the SPI85.cpp codes, there are two lines that seems to differentiate between attiny85 and attiny84.

#if defined( __AVR_ATtiny85__ )
const static uint8_t SS   = PB4;
const static uint8_t MOSI = PB1;
const static uint8_t MISO = PB0;
const static uint8_t SCK  = PB2;
#endif

#if defined( __AVR_ATtiny84__ )
const static uint8_t SS   = PA7;
const static uint8_t MOSI = PA6;
const static uint8_t MISO = PA5;
const static uint8_t SCK  = PA4;
#endif


Here are some snapshot from the datasheets as like most of you, I only 
will read through the datasheet as a last resort as they are not written for 
normal people like myself. I always wonder if the author even understand what 
he himself wrote if he reads it a few years later.



attiny x4 pinout


attiny x4 pin alternate functions
From the above info/snapshots from the datasheet, I will have to assume that whoever 
wrote the SPI85 library was just following putting support for attiny84 without actually 
tested an attiny84 before as clearly it shows the pins was incorrect.

The pin PA5, while listed as MISO (for ICSP), was also USI-DO (data out) and PA6 while 
listed as MOSI (for ICSP), was also USI-DI (data in). As stated above in Arduino 
playground, for attiny, the MISO/MOSI is used only for ICSP and not for USI-SPI.

That was easy to solve, just swapped PA5 and PA6 and everything will work fine, 
right.. but it turns out that the PA5 and PA6 is logically mapped to value of 5 and 6 
and when the codes does an Arduino code of pinMode and digitalWrite, it execute 
the wrong pins for the attiny. A Digital pin5 mapped to PA5 and Digital pin6 mapped 
to PA4 (the pin used for the SCK/USCK).. no wonder the SPI display on the 
logic analyzer goes haywire..

Once, I traced the issue and figure out the problems, I just had to hard code the Arduino digital pins
to the AVR_ATtiny84 defines as below and changed the MOSI/MISO source of confusing to 
USI_DO and USI_DI :-

#if defined( __AVR_ATtiny84__ )
const static uint8_t SS   = 3;
const static uint8_t USI_DO = 5; 
const static uint8_t USI_DI = 4;
const static uint8_t SCK  = 6;
#endif

After that, I was able to see the correct settings on the Serial Debug or the SPI patterns on the 
logic analyzer. I do not know enough about how the tiny cores was written to put the 
proper attiny84 pin names but it was finally working.

If you figure out on how to fix the attiny84 pins, please let me know. The nRF24L01 library
was located at the summary links below.

There is an example of attiny84 codes in the Mirf library at github repo.

Summary Links :-

Mirf (nRF24L01) libs for UNO/attiny84/attiny85 :- https://github.com/stanleyseow/arduino-nrf24l01
Arduino tiny-cores :- https://code.google.com/p/arduino-tiny/







Friday, June 21, 2013

Electric Imp + Arduino + nRF24L01 = Fully Internet Wireless Solution

When we discovered the Electric Imp recently, we almost immediately ordered them directly from US to do research and coding on them. You can read more about the electric imp on their websites or view their videos on Makerfaire, in short, it is a awesome product. 

electric imp



What it does is to simplify the Internet connectivity of electronic projects and the ability to program your device or "things" over the Internet on a web based IDE is a really powerful concept made into reality.

Before the Imp, a few other options existed to have your device "Internet Enabled" and they are either too expensive or requires a lot of other stuff like router / firewall configuration, having a linux box in between like the Raspberry Pi version of Raspbian or plug in an Ethernet Shield on top of an Arduino. I've tried  them all out and could not find any other solution as simple as this.

Firstly, do not be confused by it SD card looking form factor, it is NOT a memory card but a powerful microcontroller plus wifi enabled radio all stuff into a tiny SD card otter shell. When it comes to connecting endpoints like smartphones and laptops to wifi, we are all aware of the trouble of choosing the correct access point and keying in the wifi password to gain access. 

How do you key in the access point name and wifi password to this small little SD card microcontroller ? The answer is light or blinking lights to transmit these wifi authentication information directly to the card itself. They have mobile apps for both iOS and Android platform. View this video demo on how it works.

With the Internet connectivity issue easily solved using an Electric Imp, my next questions is what if I want to connect more than one device ? Should I buy another electric imp and the Imp development board to house it or should I find an alternative (or more cost effective ) method to link up all my other devices/things in the house.

With my other projects all using nRF24L01 radio, the choice is quite obvious for a wireless solution, find a nRF24L01 driver for the electric imp platform. After a few google searches, I manage to find an initial driver for the electric imp on the forum  BUT it was totally not working and unfinished. This is what usually happens to open source and community projects, someone will write the codes when time and interests permits and abandon it when it is either not working or find something else more interesting project to do...

Since I have a little bit of experience forking the nRF24L01's RF24 library and making the RF24 drivers work for Arduino, Raspberry Pi and Atmel attiny85, I took up the challenge of writing the nRF24L01 driver for the electric imp at the same time learning a C like new object oriented programming language called Squirrel that was used on the Imp.

The nRF24L01 I wrote was a combination of RF24 codes with the Mirf codes as the Mirf codes was easier to understand and was originally written in C instead of C++ for the RF24. There were some issues on the SPI but with a help of a Logic Analyzer and Logic software, I was able to fix most of the SPI related issues   

The github repo for the Electric Imp nRf24L01 driver is at https://github.com/stanleyseow/electricimp-nRF24L01. You can comment out all the debug output when everything the radio is running ok.

block diagram

My next task is to find a useful application for my wireless solution, since I had a RGB LED strip and always like to make an Internet enabled RGB lighting, I hook up everything like the block diagram above. The web portion is a jquery colour picker from farbtastic with some touchscreen add-ons for touchscreen smartphone/tablets. Do viewsource to see the javacript codes at http://stanleyseow.asuscomm.com/color/.

colour picker

The Arduino portion is a simple code to read the three RGB codes from the colour picker webpage via nRF24L01 in #RRGGBB in hex and convert them to decimal ( 0 - 255 ) for analogWrite to the PWM pins. I'm driving the 12V RGB LED strip using a ULN2803 IC similar to this page on ambient lighting.

Arduino + nRF24L01 + RGB
The electric Imp part of the hardware is an April development board with the Impee SD card with SPI pins connected to the nRF24L01 radio. Since both the Imp and nRF24L01 runs on 3.3V, I do not need power regulator for this.

Impee + nRF24L01
Those red mini hooks are connected to my logic analyzer when I was troubleshooting the SPI issues I faced earlier. 

In summary, this seems like a complex setup but in actual fact after removing all the nRF24L01 library/drivers, the lines of codes is only the below :-

Imp Server Side :-
  1. ===============================================================================================
  2. Electric Imp agent side ( cloud / server side ) :-
  3. ===============================================================================================
  4.  
  5. http.onrequest(function(req, resp){
  6.     server.log("Got a HTTP request");
  7.     if (req.method == "POST"){
  8.         local body = http.urldecode(req.body)
  9.         server.log(body.data);
  10.         device.send("agentBuffers",body.data);
  11.         //device.on(recvfromImp,impBuffer);
  12.     }
  13.     resp.send(200"<head><meta http-equiv=\"refresh\" content=\"1;url=http://stanleyseow.asuscomm.com/color/\"><html>OK</html></head>");
  14. });


Imp device to send to nRF24L01 ( radio.send() ) :-
  1. ===============================================================================================
  2. Electric Imp device ( the white wifi SDcard in yr home ) :-
  3. ===============================================================================================
  4.   function watchdog2() {
  5.         agent.on("agentBuffers",function(value) {
  6.         server.log("Got a buffer from agent/http");
  7.            if ( value.len() < 33 ) {            // Make sure nRF24L01 payload is only 32bytes or less
  8.            radio.send(value,value.len());          // Send the payload to the radio
  9.            }
  10.         } );
  11.         radio.powerRX();
  12.         imp.wakeup(1, watchdog2 );    
  13.     }

Arduino portion after getting the #rrggbb from nRF24L01 :-

  1.         HEXtoRGB(); 
  2.         analogWrite(RED_PIN,r);
  3.         analogWrite(GREEN_PIN,g);
  4.         analogWrite(BLUE_PIN,b);


Summary Links :-
- Arduino RGB code http://pastebin.com/ZvtE1p9T



Sunday, June 9, 2013

Arduino Serial over nRF24L01

While browsing for nRF24L01 solutions, I came across this UART interface to nRF24L01 that work similar to Xbee, serial data into the module RX/TX and serial data out of the module at the other end.  Since those modules uses Atmel ATmega8 as the MCU, I had an idea to write the code for it from an Arduino IDE and make a few PCBs for it. Making it work like Xbee serial would be fantastic as the cost is only a fraction compared to Xbee. I always joked that the X in Xbee stands for eXpensive.

UART to nRF24L01

For me, the drive to spring into action is usually to solve a problem in hand and during the myduino.com 50% mega sale, I purchase a Skylab GPS module to mess around with GPS stuff. I've been messing with GPS since 2005 with my purchase of the handheld color Garmin GPSmap60C but these days, GPS is no big deal since any smartphone is usually equipped with one.

Skylab GPS module

After soldering jumper cables to the Skylab Vcc,Gnd, RX & TX pins as they were not the standard breadboard size of 0.1" (2.54mm), I loaded TinyGPS library from Arduiniana and was surprise on the sensitivity of this GPS module. Inside my room, I can receive very strong signals compared to my trusty Garmin GPSmap60C or my Holux M-241 (for geotagging) where I have to stand outside my balcony for 5-10 mins just to get a GPS lock.

I just hook up a 16x2 LCD to the Arduino UNO to display the location and the number of satellite and hdop value for accuracy of the location.

Since the GPS module runs on serial and spit out data continuously, I can use this as my "content" or raw data to feed to my UART to nRF24L01 module and another module just reverse it back to Serial into an Arduino or output to Serial Monitor for a GPS software to interpret them instead of using Arduino GPS library.

Another really important piece that got me started was this blog by JHaskell on Serial communications fundamentals on Arduino that explain about using start and stop delimiters to capture serial input into an array.

With this two pieces of info, I just need to figure out on how to split up the payload to fit the nRF24L01 max payload size and reassemble it back together at the receiving end. 

I initially started with using a $ for the start delimiter and a few other symbols at the end delimiters to denote 1st fragment, 2nd fragment and last fragments on the receiving side. Greg suggested that I use a one byte header to denote fragmentation, and rest of the bits as fragment sequences and the rest as payload. I finally decide to use 2 bytes headers with the 2nd byte reverse for future use and the first byte as follows :-

8 bit    - 1 - fragment, 0 - no fragment
5-7 bit - number of fragments  
4 bit    - unused
1-3 bit - fragment number

I've been reading TCP/IP headers packets for a long time and this is the first time I wrote my own simple protocol to fragment and assembly a wireless packet and even reverse 8 bits for future use...

Here are some pictures before I continue with the rest of the story..

Arduino UNO nRF24L01 GPS module

The first thing you might be wondering is why is there a FTDI USB-Serial breakout board  when the Arduino already have a built-in USB-Serial on pin 0 & 1. This issue started when I was receiving GPS data using Arduino SoftwareSerial library and while this library works with the TinyGPS mentioned above, it was starting to drop bytes ( serial overflow ) and some of the GPS data was lost before I can even sent them over the radio frequency.

Someone at Arduino forum suggested that I use the hardware serial to receive the GPS data and plug in the FTDI USB Serial for debugging purpose/serial monitor. I tried out his suggestions and all the dropped bytes went away immediately. Since the final product do not really need to connect to Serial Monitor, I might as well use the hardware serial instead of the slow SoftwareSerial library.

Arduino UNO nRF24L01
Above is a picture of the receiver, nothing special, just an nRF24L01 with an adapter that plug directly to the Arduino pins or to a breadboard with jumper wires flying all over the place... ( ignore the 16x2 LCD, it was permanently there for other projects )

I will not go though the programming codes on this post as they are still not fully optimize but I will post the links to them on pastebin.com. Below are the output from mini GPS software displaying all the satellite signals and my location had been masked to protect my identity.

Mini GPS
Someone on Google+ asked me about using this setup ( nRF24L01+ and GPS module) on a remote control boat and I just discovered that you can use this setup to measure the range of the radio, just mark the initial GPS coordinates and the furthest distance the radio + GPS module can reached and calculate the distance using both the GPS coordinates.

*** Special thanks to Greg for helping me out on ideas, suggestions and coding the serial and nRF24L01 stuff.


Summary Links :-
- My Serial to nRF24L01 code V4 http://pastebin.com/mmMxgdCx
- JHaskell's Arduino Serial communications fundamentals
- Arduiniana TinyGPS library

Related Posts Plugin for WordPress, Blogger...