Bajdi electronics

Hobby electronics website

Browse: Page 35

Playing with Nrf24L01 modules

By Bajdi on February 5, 2012

Nrf24L01

Nrf24L01

I recently bought 4 Nrf24L01 modules, 2 small modules with internal antenna and 2 bigger ones with a preamp, amp and external antenna. You can find the small modules for less then 5$ a piece on Ebay. It’s the cheapest way make a wireless connection between 2 Arduino’s. These modules are based one the Nordic Semiconductor nRF24L01+ chip and use the SPI bus and 2 extra pins, you can find more information and how to connect them here. I found 2 libraries for these chips, the Mirf library on the Arduino playground and Maniacbugs RF24 library. I had some trouble getting these things to work, I searched the Arduino forum and it seems that I wasn’t the only one having issues with these modules and an Arduino Mega 2560. You can read about it here. After a lot of head scratching I got all 4 of the modules to work. I then tried to make some own sketches using the RF24 library and failed miserably. I have lot to learn when it comes to coding 🙁

I then tried out the Mirf library and had a bit more success. I managed to write 2 sketches (transmitter and receiver) that sent the analog value from one Arduino to the other. I think this is the minimum code you need to get them to work. Well it’s a start… I hope to use these modules to remote control a “robot”.
Here are the sketches:
The following sketch goes in to the transmitter. (I used my Arduino Mega 2560 with a potentiometer connected to analog pin A0)

// http://www.bajdi.com
// Nrf24L01 connected to Mega 2560
// Nrf24L01 connection details http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
// Transmit analog value from pin A0 to the receiver

#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

int rate;

void setup(){

  Serial.begin(9600);
  Mirf.cePin = 48;             //ce pin on Mega 2560, REMOVE THIS LINE IF YOU ARE USING AN UNO
  Mirf.csnPin = 49;            //csn pin on Mega 2560, REMOVE THIS LINE IF YOU ARE USING AN UNO
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"clie1");
  Mirf.payload = sizeof(rate);
  Mirf.config();
}

void loop(){
  rate = analogRead(A0);

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

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

  while(Mirf.isSending()){
  }
} 

The following sketch goes in to the receiver Arduino. (from this Arduino we can read the analog value through the serial monitor)

// http://www.bajdi.com
// Nrf24L01 connected to Arduino Uno
// Nrf24L01 connection details http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
// Receives analog value from transmitter and display it on serial monitor


#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

int rate;

void setup(){

  Serial.begin(9600);

  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"serv1");
  Mirf.payload = sizeof(rate);
  Mirf.config();
}

void loop(){

  while(!Mirf.dataReady()){
  }
  Mirf.getData((byte *) &rate);

  Serial.println(rate);
  delay(100);
}

With some small changes to the above sketch we can use the value read from the potentiometer (rate) and map it to a value between 0 and 255 to dim a led (analogwrite function).

// http://www.bajdi.com
// Nrf24L01 connected to Arduino Uno
// Nrf24L01 connection details http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
// Receives analog value from transmitter and maps it to a PWM range (0-255) to dim a led


#include <SPI.h>
#include <Mirf.h>
#include <nRF24L01.h>
#include <MirfHardwareSpiDriver.h>

int rate;
int ledValue;

const int led = 3;

void setup(){

  Serial.begin(9600);
  pinMode(led, OUTPUT);
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"serv1");
  Mirf.payload = sizeof(rate);
  Mirf.config();
}

void loop(){

  while(!Mirf.dataReady()){
  }
  Mirf.getData((byte *) &rate);
  ledValue = (map( rate, 0, 1024, 0, 255 ) );
  analogWrite(led, ledValue);
  Serial.println(ledValue);
  delay(10);
}

Posted in Arduino sketches | Tagged nRF24L01 | 29 Responses

RGB led color mixing

By Bajdi on February 4, 2012

Arduino RGB led color mxing from Bajdi on Vimeo.

The above video is what you get when you connect 3 RGB leds to an Arduino Mega 2560. I covered them with the diffusion cover from my Nikon flash to get a better light diffusion. RGB leds are fun things, you can make some cool visual effects with a couple of these leds. I got my inspiration from here.

Posted in Arduino | Tagged RGB LED | 1 Response

DHT22 datalogging

By Bajdi on February 4, 2012

Datalogging dht22 to SD card

By connecting a DHT22 temperature and humidity sensor, a DS1307 RTC module and an SD card module to the Arduino Mega 2560 we can make a data logging system. The DHT22 sensor measures the temperature, the DS1307 gives us the date and time and this data is then written to the SD card over the SPI bus. The values from the DHT22 sensor and the DS1307 are comma separated so you can easily import the data in a spreadsheet. Was straightforward to get to work. The data is written every 3 seconds to the SD card, I just used the “delay” function. Should use the time from the DS1307 the next time. I tried adding an ethernet module to the mix but I can’t get the SD card and the ethernet module to work together on the Mega 2560, although separately they work.

The sketch:

/*
  http://www.bajdi.com
  SD card datalogger for Arduino Mega (this sketch will ONLY work with Arduino Mega) 
 
 This example shows how to log data from a DHT22, to an SD card using the SD library.
 Time stamp is taken from a DS1307.
  	
  * SD card attached to SPI bus as follows:
 ** MOSI - pin 51
 ** MISO - pin 50
 ** CLK - pin 52
 ** CS - pin 53
   	 
 */

#include <Wire.h>
#include "RTClib.h"
#include <dht.h>
#include <SD.h>
dht DHT;

RTC_DS1307 RTC;

#define DHTTYPE DHT22 // Sensor type
#define DHT22_PIN 2   // DHT22 signal on pin 2

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
const int chipSelect = 53;

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
  RTC.adjust(DateTime(__DATE__, __TIME__));
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(53, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
}

void loop()
{
  DateTime now = RTC.now();
  
  int chk = DHT.read22(DHT22_PIN);
  
  File dataFile = SD.open("datalog.csv", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.print(now.day(), DEC);
    dataFile.print('/');
    dataFile.print(now.month(), DEC);
    dataFile.print('/');
    dataFile.print(now.year(), DEC);
    dataFile.print(" , ");
    dataFile.print(now.hour(), DEC);
    dataFile.print(':');
    dataFile.print(now.minute(), DEC);
    dataFile.print(" , ");
    dataFile.println((float)DHT.temperature);
    dataFile.close();
    // print to the serial port too:
    Serial.println(DHT.temperature);
  }  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.csv");
  }
  delay(3000); 
}

Posted in Arduino sketches | Tagged DHT22 | 5 Responses

« PreviousNext »

Recent Posts

  • 3D Printer extruders
  • Mearm forked
  • Arduino time lapse – camera pan device
  • Bought a 199$* Prusa I3 3D printer
  • Simple DIY lab power supply
  • Bajduino
    • Bajduino 1284
    • Bajduino 328
  • Stepper robot arm

Recent Comments

  1. 200+ Arduino Projects List For Final Year Students - He Loves Math on Obstacle avoiding robot made from cheap parts
  2. MP3 Alarm Clock: Crafting Customized wake-Up tunes with Arduino on Arduino MP3 alarm clock
  3. Bas on L9110 H-bridge module
  4. Victor on L9110 H-bridge module
  5. WAQAR AHMED on Dimming led with IR remote control

Archives

  • June 2016
  • August 2015
  • May 2015
  • January 2015
  • December 2014
  • November 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • September 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011

Categories

  • 3D printing
  • Arduino
  • Arduino sketches
  • Bajdupod 996R
  • Bajdupod 9G
  • Bob the biped
  • Buying parts
  • Dagu Rover 5
  • Dagu service droid
  • Gentoo
  • Insectbot
  • Line following robot
  • Raspberry Pi
  • Self balancing bot
  • Bajduino
    • Bajduino 1284
    • Bajduino 328
  • Stepper robot arm

Copyright © 2026 Bajdi electronics.

Powered by WordPress and Hybrid by Bajdi.