Sunday, December 22, 2013

Grove Button Tutorial

The Grove Button is a handy little component which simplifies the push-button experience. It doesn't take much programming to get this component to work. And while the button works extremely well with the Grove Base Shield, we will be connecting this button directly to the Arduino UNO.

The button will be LOW in its normal resting state, and report HIGH when the button is pressed. Have a look at the video below to see this project in action.

Video






Parts Required




Sketch







Arduino Sketch




 1
2
3
4
5
6
7
8
9
10
11
12
/* Grove Button Sketch - Written by ScottC 22nd Dec 2013 
http://arduinobasics.blogspot.com
--------------------------------------------------------- */

void setup(){
pinMode(8, INPUT);
pinMode(13, OUTPUT);
}

void loop(){
digitalWrite(13, digitalRead(8));
}

The signal pin of the Grove Button attaches to digital pin 8 on the Arduino, and the LED is connected to digital pin 13 on the Arduino. When the button is pressed, it will send a HIGH signal to digital pin 8, which will turn the LED on. When the button is released, the signal will change to LOW and the LED will turn off.





Friday, December 20, 2013

PIR Sensor (Part 2)

PIR Border Title
In this tutorial we will connect our HC-SR501 PIR (movement) Sensor to an Arduino UNO. The PIR sensor will be powered by the Arduino and when movement is detected, the PIR sensor will send a signal to Digital Pin 2. The Arduino will respond to this signal by illuminating the LED attached to Pin 13.

PIR Sensor (Part 1) : Showed that this sensor can be used in isolation (without an Arduino). However, I will still demonstrate how you can attach this sensor to the Arduino so that we can move forward to more advanced objectives and concepts.


Video









Parts Required






Fritzing Sketch



PIR Tutorial 2_bb




Arduino Sketch




 1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*Simple PIR sketch: Written by ScottC, 19th Dec 2013

http://arduinobasics.blogspot.com/

----------------------------------------------------*/

void setup(){
pinMode(13,OUTPUT);
pinMode(2,INPUT);
}

void loop(){
digitalWrite(13,digitalRead(2));
}




The sketch above reads the signal coming in from the PIR sensor on Pin 2, and if it reads HIGH, it light up the LED attached to Pin 13. If it reads LOW, it will turn the LED off. This is all controlled by line 13 in the Arduino Sketch above.

The following table helps to identify the purpose of the potentiometers on the PIR sensor. Most people say they use trial and error. I will attempt to reduce the mystery of these components on the PIR board.


104 (Left) – Max


104Left
LED on = 20 sec
LED off = 3 sec

When you move the 104 labelled potentiometer all the way to the left (max position), the LED will remain on for 20 seconds after movement is detected. The 20 seconds is independent of the other potentiometer (105) setting. When the LED turns off, it will remain off for 3 seconds before the sensor will trigger again from any further movement.




104 (Right) – Min



104Right

LED on = 1 sec
LED off = 3 sec

When you move the 104 labelled potentiometer all the way to the right (min position), the LED will remain on for 1 second after movement is detected. When the LED turns off, it will remain off for 3 seconds before the sensor will trigger again from any further movement.




105 (Left) – Max



105Left

Most sensitive – Detects movement from over 10 steps away.

The 105 labelled potentiometer controls the sensitivity of the PIR sensor. When in the left position, the PIR sensor is most sensitive and small amounts of movement will trigger the sensor. It detected my movement (ie a single step to the left of right) from over 10 steps away from the sensor. I was very impressed.




105 (Right) – Min



105Right

Least sensitive: Need significant movement for sensor to trigger. Detects movement from about 4 steps away.

When the 105 labelled potentiometer is twisted to the right, the PIR sensor becomes a lot less sensitive. I needed to take much bigger steps to the left or right to trigger the sensor (about 3 times the size compared to the left position). It also had a lot more trouble detecting movement occurring further away. It only really started to detect my movement when I was about 4 steps away from it. 

My preferred combination was 104-Right (min) + 105-Left (max), which meant that the sensor would remain on for only a short period of time, and detect any subtle movements in the room. This combination is displayed below:


IMG_0697

I have not tested to see how it performs over a very long period with this setting, and whether it would suffer from false positive readings, but that could easily be fixed by turning the 105 labelled potentiometer a bit more to the right.




Monday, December 16, 2013

PIR Sensor (Part 1)

PIR Title

PIR sensors are pyroelectric or “passive” infrared sensors which can be used to detect changes in infrared radiation levels. The sensor is split in half, and any significant difference in IR levels between the two sections of the sensor will cause the signal pin to swing HIGH or LOW. Hence it can be used as a motion detector when IR levels move across and trigger the sensor (eg. human movement across a room).
The potentiometers are used to adjust the amount of time the sensor remains “on” and “off” after being triggered.  Essentially the delay between triggered events.
Here are a couple of pictures of the PIR sensor.

IMG_0659  IMG_0654  IMG_0648 IMG_0658

The sensor used in this tutorial is HC-SR501 PIR sensor.
You can get more information about this sensor here.




Parts Required







Sketch

 

PIR1_Fritzing Sketch




IMG_0669






Video

 






 

Sketch Explanation

 

The sketch described above can be used to test the functionality of the PIR sensor. I had another one of these sensors in my kit, and could not get it to work, no matter what I tried. The sensor would blink continuously even when there was no movement in the room. However, I must warn you, this specific sensor has an initialisation sequence which will cause the LED to blink once or twice in a 30-60sec timeframe. It will then remain off until the sensor detects movement. The amount of time that the LED remains on (when movement is detected) is controlled by one of the potentiometers.

Therefore, you could have it so that the LED blinks quickly or slowly after movement is detected.
If you set it to remain off for a long time, the sensor may appear to be unresponsive to subsequent movement events. Getting the timing right is mostly done out of trial an error, but at least the board indicates which side is “min” and which side is “max”.
Have a look at the PIR picture above for the potentiometer positions/timings that I used in the video.




In a future tutorial, I will connect this sensor to the Arduino. But don’t worry. The sketch is just as easy. And then the real fun begins.

See PART 2 - Connecting a PIR to an Arduino




Thankyou

 

I would like to thank the following people who took time out to help me when I was having issues with this sensor:
  • Steven Wallace
  • Bobby Slater
  • Pop Gheorghe
  • Mike Barela
  • Winkle ink
  • Jonathan Mayer
  • Don Rideaux-Crenshaw
  • Ralf Kramer
  • Richard Freeman
It just shows how great the maker community is. Thanks again… I almost gave up on this one !















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


Tuesday, August 13, 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/







Wednesday, July 24, 2013

PPM Output (Draft - Looking for testers)

If we have code capable of generating servo signals its very simple to adapt this to generate a PPM output instead.

In a previous post we looked at reading an incoming PPM signal and we saw how the PPM Signal describes the individual servo pulses -
http://rcarduino.blogspot.ae/2012/11/how-to-read-rc-receiver-ppm-stream.html

To generate the PPM Stream from a set of servo commands all we need to do is change our code to toggle a single pin instead of individual pins for each servo.


If we look at the point where the channel 1 pulse ends and the channel 2 pulse starts in the diagram above. The code needs to find which pin is associated with the current chanel, then set the pin low to end the pulse. It then finds the next channel, sets its pin high and then sets the timer compare register so that a interrupt routine will be called again when its time to end the new channels pulse and start the next channels pulse.

To output PPM its actually a lot simpler. We are always dealing with the same pin so all we need to do is set the pin high, set the compare register so that the interrupt is called again to when we need start the next channel pulse and then we immediately set the PPM pin low again and exit. Systems which expect a PPM Input are only interested in the rising edge of the PPM Pulse so once we have set it high (giving the external system the rising pulse edge it is looking for) we can immediately set it low again and get out.

Timers, Compare Registers, Service Routines

The following posts provide an indepth background to how the standard Arduino Servo library uses timers, compare registers and interrupt service routines to control upto 12 Servos. The RCArduinoFastLib uses the same approach with a number of optimizations including support for more servos, faster refresh rates, dual refresh rates and reading incoming PPM signals. The remainder of this post presents a small modification of the RCArduinoFastLib which will provide a PPM output instead of individual servo channels.

Background posts
Standard Arduino servo library overview
http://rcarduino.blogspot.ae/2012/01/can-i-control-more-than-x-servos-with.html

RCArduinoFastLib
http://rcarduino.blogspot.ae/2012/11/how-to-read-rc-channels-rcarduinofastlib.html
http://rcarduino.blogspot.ae/2012/11/how-to-read-rc-receiver-ppm-stream.html

The Modified RCArduinoFastLib with PPM Output
Looking for testers, PM me Duane B on the Arduino forum for a zip folder to build the library and test sketch.





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