Overview:
Have you ever wondered how the controller controls the toy car in a range with no wires present between the controller and the toy car? It is because the controller acts as the transmitter and the toy car as the receiver, the controller sends data into the toy car and the toy car receives the data and acts as what the controller tells what to do. Confusing right? There are so many wireless modules out there that you can use in order to perform this transmitter and receiver.
So in this activity, we are going to test how to NRF24l01 works. This activity is a little bit complicated for beginners, but if you just read and follow my instructions I assure you that everything will be functioning. So in order to test, we are going to control two servos. Honestly, this module is hard to deal with. According to my research there are so many issues with this module, but don’t you worry guys I will teach you on how to deal this problems.
Hardware Used:
- 2- Breadboard
You can buy all this Hardware at Createlabz.
Software Used:
Library Used:
Application Description:
I am using this kind of module in order to send and receive data which means you need two of these for receiver and transmitter. It has a 2.4GHz band and can operate baud rates at 250 kbps to 2Mbps. If used in open space the range of this module can operate on 92 meters. The current consumption of this module is at 12mA during transmission. The voltage is from 1.9v to 3.6v.
The module can use 125 different channels which give a possibility to have a network of 125 independently working modems in one place. Each channel can have up to 6 addresses, or each unit can communicate with up to 6 other units at the same time.
Problems Encountered:
Many users have had trouble getting the nRF24L01 modules to work. Many times the problem is that the 3.3V Power to the module does not have enough current capability, or current surges cause problems. Here are suggestions:
Use the RF24 Library from TMRH20 (below) and set power low to minimize power requirements: radio.setPALevel(RF24_PA_MIN); Space the two radios about a meter apart. After you have things working, and if you know you have enough 3.3V current (up to 250 mA or more) then try higher power. The possibilities are: RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
Connect a 3.3 uF to 10 uF (MicroFarad) capacitor directly on the module from +3.3V to Gnd (Watch + and – !) [Some users say 10 uF in parallel with 0.1uF is best] This is especially important if you are connecting the module with jumper wires. Or you are using the regular Arduino UNO which provides only 50 mA at 3.3V.
There are also nice low-cost base modules like this
that you can plug nRF24L01 modules into. These have a 3.3V regulator built in, AND good power bypass capacitors. You can also find these on https://createlabz.store/
Set up the Hardware:
Transmitter
Receiver
NOTE!!!!!!
If you are NOT using a AM1117 then the VCC of your NRF24l01 should be always 3.3v.
Code:
Transmitter.ino
#include <SPI.h> #include <nRF24L01.h> #include <RF24.h> RF24 radio(7, 8); const byte rxAddr[6] = "00001"; int sendingdata[6]; void setup() { Serial.begin(9600); radio.begin(); radio.setPALevel(RF24_PA_MAX); radio.setAutoAck(false); radio.setRetries(15, 15); radio.openWritingPipe(rxAddr); radio.stopListening(); delay(50); } void loop() { int in1 = analogRead(A0); //Right Stick Up and Down int in2 = analogRead(A1); //Right Stick Left and Right // int in3 = analogRead(A2); //left Stick Up and Down // int in4 = analogRead(A3); //Left Stick Left and Right //int in3 = 22; //left Stick Up and Down //int in4 = 22; //Left Stick Left and Right sendingdata[0]=in1; sendingdata[1]=in2; //sendingdata[2]=in3; //sendingdata[3]=in4; radio.write( sendingdata, sizeof(sendingdata) ); Serial.print("RY ="); Serial.println(sendingdata[0]); Serial.print("LY ="); Serial.println(sendingdata[2]); delay(50); }
Receiver.ino
#include <SPI.h> #include <RF24.h> #include <Servo.h> RF24 radio(7, 8); const byte rxAddr[6] = "00001"; //SErvo control Servo yaw; //Servo pitch; //Servo roll; Servo thr; //Joystick ARray int joystick[6]; void setup() { for(int i = 2; i<=4; i++){ pinMode(i,OUTPUT); } while (!Serial); Serial.begin(9600); radio.begin(); radio.setPALevel(RF24_PA_MAX); radio.setAutoAck(false); radio.openReadingPipe(0, rxAddr); radio.startListening(); //Servo attachments thr.attach(2); yaw.attach(3); //pitch.attach(4); //roll.attach(5); delay(50); } void loop() { if (radio.available()) { bool done = false; while (!done) { // Fetching the data payload radio.read( joystick, sizeof(joystick) ); done = true; int val0=map(joystick[0],0,1024,0,180); int val1=map(joystick[1],0,1024,0,180); //int val2=map(joystick[2],0,1024,0,180); //int val3=map(joystick[3],0,1024,0,180); thr.write(val0); yaw.write(val1); //pitch.write(val2); //roll.write(val3); //for serial observation Serial.println(val0); Serial.println(val1); //Serial.println(val2); //Serial.println(val3); } } else{ Serial.println("Data not received"); } delay(50); }
Code Breakdown:
RF24 radio(7, 8); // CE, CSN
So we need to include the basic SPI and the newly installed RF24 libraries and create an RF24 object. The two arguments here are the CSN and CE pins.
const byte address[6] = "00001";
Next we need to create a byte array which will represent the address, or the so called pipe through which the two modules will communicate.
radio.openWritingPipe(address);
On the other side, at the receiver, using the radio.setReadingPipe() function we set the same address and in that way we enable the communication between the two modules.
radio.openReadingPipe(0, address);
Then using the radio.setPALevel() function we set the Power Amplifier level, in our case I will set it to minimum as my modules are very close to each other.
radio.setPALevel(RF24_PA_MIN);
Note that if using a higher level it is recommended to use a bypass capacitors across GND and 3.3V of the modules so that they have more stable voltage while operating.
radio.stopListening();
Next we have the radio.stopListening() function which sets module as transmitter, and on the other side, we have the radio.startListening() function which sets the module as receiver.
Conclusion:
If you are interested in Wireless Communication Projects, I recommend you use this Module for 2 reasons. First, this module is one of the cheapest on the market going for as a little as 1 dollar a piece or less. Lastly, easy to use because there are many existing projects on this module in which you can combine those projects and form a good and unique one. You can also use this as a reference to your upcoming Wireless Communications Projects.
References:
http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
https://store.createlabz.com/?s=nrf24l01&post_type=product
The post Wireless Control (NRF24L01) of Servo Motors Using Joystick module appeared first on CreateLabz.