Wednesday, March 6, 2013

Migrated RF24 codes to github

I've migrated my previous static codes to github's RF24 libs at https://github.com/stanleyseow/RF24
This is a fork from gnulnulf codes that uses dynamic rf24 RPi libraries...

This will be using the Arduino maniacbug's RF24 libraries in additional to the Raspberry Pi codes under a different folder...

Changes to take note from my initial instructions  :-

- CE is now using GPIO25
- CSN is now using GPIO8 (CE0)

Thanks to gnulnulf, you can specify the devices inside of the codes as below :-


RF24 radio("/dev/spidev0.0",8000000 , 25);

The files are located at RF24/librf24-rpi

Instructions on using github version for RPi :-

Using git, issue the following instructions :-

git clone https://github.com/gnulnulf/RF24.git
$ cd RF24
$ cd librf24-rpi/librf24
$ make                                <--- Compile the files
$ sudo make install             <--- This will install the librf24.so.1.0 into the shared libraries
$ sudo ldconfig -v | grep librf

     librf24.so.1 -> librf24.so.1.0

$ cd examples
$ make

To run the programs, type sudo ./rpi-hub or sudo ./scanner

*** You need sudo to access the spidev0.0 device
Instruction on Arduino :-

Got examples/RF24/rpi_hub_arduino

Compile and upload to the Arduino

Connect a small buzzer to digital Pin2

If everything is working, you should hear a short beep ...


Summary Links :-

https://github.com/stanleyseow/RF24 ( Arduino RF24 and RaspberryPi libs codes all in one )












Wednesday, February 27, 2013

Made a slot for NRF module directly onto Arduino digital pins


Once I ran out of jumper cables, I've made a slot to hold the nRF module that sits directly onto Arduino UNO digital pins.

I've change the CSN pin from pin7 to pin9 and swap the LCD pin9 to pin7. This is reflected in my latest codes below.


// Set up nRF24L01 radio on SPI pin for CE, CSN ( *** older codes uses Pin 7 instead )
RF24 radio(8,9);


LiquidCrystal lcd(10, 7, 3, 4, 5, 6);
// Make way for the SPI pins
// 10 -> LCD 4
// 7  -> LCD 6
// 3  -> LCD 11
// 4  -> LCD 12
// 5  -> LCD 13
// 6  -> LCD 14



Arduino with nRF slot directly to digital pins


I've re-wrote both the program on the Raspberry Pi and Arduino to make it more practical use. I've made the Raspberry Pi as the central hub to receive sensor data from Arduino nodes and returned back the same data to the sender for calculating the round-trip time (rtt).

You can download the updated codes at the summary links below.

I'm using all the pipes for each of the nodes, so if you want to use this codes, uncomment the pipes for different nodes to send payload to the Raspberry Pi using different node address.


// Radio pipe addresses for the 2 nodes to communicate. Uncomment for the active node
 const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
// const uint64_t pipes[2] = { 0xF0F0F0F0E2LL, 0xF0F0F0F0D2LL };
// const uint64_t pipes[2] = { 0xF0F0F0F0E3LL, 0xF0F0F0F0D2LL };
// const uint64_t pipes[2] = { 0xF0F0F0F0F1LL, 0xF0F0F0F0D2LL };
// const uint64_t pipes[2] = { 0xF0F0F0F0F2LL, 0xF0F0F0F0D2LL };
// Pipe0 is F0F0F0F0D2 ( same as reading pipe )


*** You can either connect a small buzzer or LED to pin2 for audio/visual acknowledgement the payload once it is returned and matched the outBuffer payload sent to the RPi.


Summary Links :-

- RF24 libs/codes on github:-  https://github.com/stanleyseow/RF24 ( all Arduino & Raspberry Pi codes are on github )




Tuesday, February 19, 2013

Setup Nordic nRF24L01 RF modules to both Arduino UNO and Raspberry Pi


In my process of building a Wireless Sensor Network, I obtained  a few low cost RF module from Inhaos that are compatible with Nordic Semiconductor nRF24L01. My plans are to have these low cost RF modules replaces the wires for my sensor network on Arduino and connect them to Raspberry Pi as the IP gateway to the rest of the Internet.

Raspberry Pi with nRF24L01-PA-LNA with BT module and USB Wifi


This entry will show you on how to setup both Arduino UNO & Raspberry Pi to connect to these nRF24L01 modules.

Some basic information on these low cost RF modules, they communicate using SPI to the microcontroller and works on raw speed of 250Kbps, 1Mbps and 2Mbps.

I'm using the following hardware & software:-


Raspberry Pi Model B running Adafruit's Occidentalis V0.2 ( http://learn.adafruit.com/adafruit-raspberry-pi-educational-linux-distro/occidentalis-v0-dot-2 ) that support SPI.

Nordic Semiconductor Datasheet / Product Info ( http://www.nordicsemi.com/eng/Products/2.4GHz-RF/nRF24L01P )

RF modules used here :-
------------------

nRF24L01+ PA+LNA+2dBi ant

nRF24L01+ with PA & LNA and 2 dBi antenna ( http://www.elecfreaks.com/wiki/index.php?title=2.4G_Wireless_nRF24L01p_with_PA_and_LNA ), these are long range RF modules acting as hub on the Raspberry Pi.

Inhaos RF2400P


Inhaos RF2400P (with 5dBm RF power output) on BEKEN Chipset ( http://www.inhaos.com/product_info.php?products_id=35 ). They are compatible with the nRF24L01 but with higher output power of 5dBm.

nRF24L01+


Too many to choose from, just google for the above keyword on eBay or choose the cheapest as they sells in bulk of 10 pieces for a very low cost...

*** The main differences between the regular nRF24L01+ modules and the Inhaos RF2400P modules are that the RF2400P have a higher output power of 5dBm but could not do 250Kbps speed.

I'm running my setup using 1Mbps speed.

Setting up Arduino 

Connect using jumper wires from the above pin to the nRF24L01+ modules
Upload the sketches/program to the Arduino, download it here from Dropbox. 


Arduino UNO with nRF24L01+/PA/LNA/2dBi ant


Arduino connections to the nRF24L01 modules :-

Arduino Pin 11 to RF Module Pin 6 ( MOSI)
Arduino Pin 12 to RF Module Pin 7 ( MISO )
Arduino Pin 13 to RF Module Pin 5 ( SCK )
Arduino Pin 7 to RF Module Pin 4 ( CSN )
Arduino Pin 8 to RF Module Pin 3 ( CE )
Arduino 3.3V to RF Module Pin 2 ( VCC / 3.3V ) *** RF Module can only take 3.3V 
Arduino GND to RF Module Pin 1 ( GND )
RF Module Pin 8 (IRQ) is not connected

*** All the other pins are 5V tolerant

Optional LCD Module to display the received payload & payload length :-

I'm using Arduino Pin 7 & 8 for the SPI because my LCD module is using the following pins :-

Arduino Pin 9 - LCD Pin 4
Arduino Pin 10 - LCD Pin 6
Arduino Pin 3 - LCD Pin 11
Arduino Pin 4 - LCD Pin 12
Arduino Pin 5 - LCD Pin 13
Arduino Pin 6 - LCD Pin 14

Arduino Pin 2 - connect to buzzer ( for sound feedback when it received a valid packet )


Setting up Raspberry Pi

Upgrade your Raspberry Pi firmware to Adafruit Occidentalis V0.2 that support hardware SPI.

Download the above firmware and flash your SD card according to the instruction on Adafruit/Raspberry Pi website.

Refer to the two Raspberry Pi GPIO pinout and the physical pinout.

Raspberry Pi GPIO
GPIO with pin functions


RPi GPIO9    (Pin 21)  to RF Module Pin 7 ( MISO )
RPi GPIO10  (Pin 19)  to RF Module Pin 6 ( MOSI )
RPi GPIO 11 (Pin 23)  to RF Module Pin 5 ( SCK )
RPi GPIO8    (Pin 24)  to RF Module Pin 3 ( CE )
RPi GPIO 25 (Pin 22)  to RF Module Pin 4 ( CSN )
RPI 3.3V       (Pin 17)  to RF Module Pin 2 ( VCC/3.3V )
RPi Gnd        (Pin 25)  to RF Module Pin 1 (GND)



Instructions on using github version for RPi :-

Using git, issue the following instructions on Raspberry Pi command prompt :-

$ git clone https://github.com/stanleyseow/RF24.git
$ cd RF24
$ cd librf24-rpi/librf24
$ make                                <--- Compile the files
$ sudo make install              <--- This will install the librf24.so.1.0 into the shared libraries
$ sudo ldconfig -v | grep librf
     librf24.so.1 -> librf24.so.1.0

$ cd ../examples/
$ make

To run the programs, type 

$ sudo ./rpi-hub 
or 
$ sudo ./scanner

*** You need sudo/root to access the spidev0.0 device

Instruction on Arduino :-

Got examples/RF24/rpi_hub_arduino ( make sure the pins matches the above Arduino pinout )

Compile and upload to the Arduino

Optional :- Connect a small buzzer to digital Pin2 to hear a beep when the packet is returned to sender


If everything is working, you should see a screenshot below :-

rpi-hub screenshot


Summary Links :-

- Raspberry Pi firmware :- Adafruit Occidentalis-v0.2

- RF24 library (Arduino & RPi libs) on Github https://github.com/stanleyseow/RF24

Datasheets :-
- Nordic nRF24L01+ Datasheet :- nRF24L01+ DS

Forum :-
- Raspberry Pi Forum on nRF24L01 Click here
- Arduino Forum on maniacbug nRf24L01 driver Click here


Sunday, April 3, 2011

Controlling fan or motor speed with PWM

On the first part, I talk about switching any 12V DC or higher electronic components ON or OFF. This can be easily done using an optoisolator and a 12V reed relay. In this article, I will be using a different component to control the speed of the 12V fan or motors using Pulse Width Modulation (PWM).

The TIP-122 are Darlington transistors that can support voltage up to 100V. It consist of two transistors with resistors and diode all inside a TO-220 package. You can use TIP-120 that support up to 60V. Please refer to the TIP-120/TIP-122 datasheets for details specifications here.

TIP-122
I've wired u my circuit like the schematic diagram below :-

TIP-122 fan speed control

The input of the circuit is using analogRead(0) to read the light level from the light dependent resistors (LDR). Depending on the light level, the value is converted by the ADC to 0 to 1023. Since the PWM can only take value of 0-254, I will divide the ADC value by 4  to match the PWM value.

The 12V DC supply is separate from the Arduino 5V supply but shared a common ground.

The LCD is to display the LDR value for debugging purpose. Cover the LDR or shine a light to it to see the fan speed change.

Arduino Pin 9 (PWM) connect to the TIP-122 Base (B) pin.
The 12V fan connect to the TIP-122 Collector (C) pin and the Emitter (E) pin connect to the Ground.

Sketch to control the 12V fan speed using PWM is as below :-

   int ldr = 0;
   ldr = analogRead(0)/4;


  // if light level is room light, turn the fan speed higher

  if ( ldr < 125 && ldr > 40 ) {
    ldr = ldr + 50;
  }


  // if value is less than 40, do not switch the fan ON

  if ( ldr < 40 ) {
    ldr = 0; 
   }
  
  // else run the fan speed according to light level up to 255


  // send PWM value to TIP-122 base pin
  analogWrite(9,ldr);
  delay(500);



Friday, March 4, 2011

Controlling a 12V DC motor/fan with an Arduino

Most of the Arduino runs on 5V and driving low power actuators like LED is fine with a  resistor but how do I control relays / motors / pump / fans that runs more than 5V ?

I get a lot of beginner questions like below :-

1. How do I switch ON & OFF  a 12V DC fan from an Arduino ?
I was told I need a relay, what type of relay should I get ?

2. How do I control the speed of a 12V DC motor or fan from Arduino ?

Let's look at these scenarios one by one so that I can explain them in more details. Firstly, even if a motor or pump or fan (anything with coils inside) is 3V or 5V, do NOT power it directly from the Arduino pins as the Arduino pins can only supply a maximum of 40mA. As per my previous article on Arduino pins, they are not designed to drive a DC motor or coils. 

Always use a separate power source to drive these high load components and have a relay to switch them.

Question 1:-

How do I switch ON & OFF a 12V DC fan from an Arduino ? 
I was told I need a relay, what type of relay should I get ?

A 12V DC fan or motor have coils inside it. You would need a relay to control it. A relay is an electrically operated switch to turn on or off a device. Most relay use an electromagnet to operate a switching mechanism mechanically. You could hear a "click" when the relay switches on when it magnetize the coil and turn on the switch. Below is an image of a 12V Single Pole Double Throw (SPDT) relay housed in a transparent casing. You can see the copper wires winding or coil clearly.

12V SPDT relay

The advantage of a relay is that they can allow high flow of current and voltage across them but the disadvantage of a relays is that it is a mechanical device with springs, so the switching speed is much slower than semiconductors ( called switching speed in the specs ) and have wear and tear of the mechanical parts. They are also more expensive to manufacture compared to semiconductors switches.

Below is my sample circuit using an Arduino to drive a 12V DC Fan with a 12V relay.

- Arduino
- optoisolator 4N35
- 12V SPDT Relay
- 12V PC fan

4N35 optoisolator


An optoisolator is a chip that combined two circuits ( a LED & NPN phototransistor ) into an integrated circuit to isolate two different voltage. The Arduino is on 5V and the relay & DC fan is on 12V. According to the 4N35 datasheets, it can support up to 70V from pin 4 to 5.

See the circuit diagram below :-

Arduino 4N35 12V Relay

When pin 12 is turn ON or HIGH, it will triggered the LED between pin 1 & 2 inside the 4N35 optoisolator (6 pins white chip) to turn on the phototransistor ( pin 4 & 5) and  complete the circuit and send 12V power into the relay (black colour). When the coil is energize, you will hear a click and switch on the motor. The red wires are connected to 12V power supply.

Using this method, it can only switch on or off a circuit but could not control the variable speed of the motor. A second method will use a semiconductor, NPN Power Transistor model TIP122 to control the speed of the DC motor using PWM from pin 9.

On Part 2 of the article, I will explain on how to control the speed of a 12V DC fan using PWM.

Friday, February 11, 2011

Why the Arduino won and Why its here to stay

For a beginner, if you are still considering to use Arduino vesus other microcontrollers, Phillip Torrone, Creative Director of Adafruit on Make: Online wrote an interesting article on "Why the Arduino won and Why its here to stay" that might just be able to help tip you over.

The author also commented that it only takes 10 minutes, assuming your Internet download is fast, to get Arduino up and running and that itself show the simplicity of Arduino.

From reading the article, it remind me of some of the things I told my readers when choosing between different types of Arduino. I usually advise beginner that "Unlike the PC CPU where faster and more Ghz is better, in microcontroller, less is good, uses less power and more power efficient, you can even run it off batteries".

To keep the Arduino cost low with a lower entry barrier, everything else is modular or called a breakout board or "shields" in Arduino term like LCD module, network or TCP/IP module, realtime clock module, GPS module and many other modules. Again, less is good and keep it simple concept.

I find most challenging part for beginners is actually understanding the datasheets, Sparkfun wrote a good article on how to Read a Datasheet, well, we can read english but understand engineer's terminology is another challenge for beginners as most datasheets are written by engineers for engineers.

Finding the correct components is the next most asked questions, like "How do I control a 12V or 220V from an Arduino ?" What parts do I need to accomplished this task ? I will address this in my next blog entry.

One of the recent events I think "Arduino won" is the worldwide shortage of the Atmega328 DIP version of the chip from Atmel. DIP (stands for Dual In-line Package) is a through-hole packaging of the chip that can be plugged into a breadboard or IC (Integrated Circuit, technical name for chips) sockets and can easily be removed and replaced if you burned your chip. The original version of Arduino have all the components in through-hole and the latest version, Arduino UNO moved most of the components into surface mounted except for the main Atmega328 chip. With the recent shortage, to fulfill the hugh demand, Arduino made a surface mounted version of Arduino UNO. The DIP version of the chip is important because hobbyist like myself, like to make parmanent version of Arduino for different type of projects.

All these food related names like DIP, chips, bytes and nibble just makes me hungry.

















LinkWithin

Related Posts Plugin for WordPress, Blogger...