Arduino control using NRF24L01 2.4ghz transmitter & receiver module

MickyF

Registered
Well I probably should have got it working first... But I built the transmitter with a detent pot, 6 push buttons (for sound) and Arduino Nano with built in RF (24L01). I'm pleased with the physical result and the transmitter functions great but I can't find info on how to send my push buttons signals and speed (0-255) data to the receiver. I do have some basic programming skills if only could find the info. I found code examples on several sites but don't really understand how it works! Has anyone been down this route? Plenty of time on hands now to sort this with little money!
 

Attachments

  • IMG_20200415_220504_resized_20200415_100621223.jpg
    IMG_20200415_220504_resized_20200415_100621223.jpg
    238.8 KB · Views: 0
  • IMG_20200415_220521_resized_20200415_100620938.jpg
    IMG_20200415_220521_resized_20200415_100620938.jpg
    270.1 KB · Views: 0
Yes, you will need lots of time :)

I now use the ESP8266 but started with the Arduino Nano and NRF24L01 many years ago when I had to use separate components. I hadn't ralised that they are now available combined.

At that time there were 2 libraries, and I choose one called Mirf. Libraries like this should contain a transmit and receive example. You use these examples to get it working between two Arduinos and then modify it to suit your purpose. I'm sure the libraries are different and improved now so can't advise which one to use.

It will be the same for the rotary potentiometer, search for an example and use that within your own code.

This was my first Arduino project and dispite programming Rockwell 6502 8 bit processors in the 1980s in machine code, I wasn't really a programmer.I did however like the simplicity of the code structure (initialisation, setup and main loop) and the Arduino IDE was very easy to use. Much simpler, in my opinion, than Python for example on a Raspberry Pi.

One piece of advise, never use the delay() function. Everything stops during this time. If you want to turn an LED on for 2 seconds, switch it on, and use a timer within the main loop to switch it off again after 2 seconds.

Good luck!
 
Yes, you will need lots of time :)

I now use the ESP8266 but started with the Arduino Nano and NRF24L01 many years ago when I had to use separate components. I hadn't ralised that they are now available combined.

At that time there were 2 libraries, and I choose one called Mirf. Libraries like this should contain a transmit and receive example. You use these examples to get it working between two Arduinos and then modify it to suit your purpose. I'm sure the libraries are different and improved now so can't advise which one to use.

It will be the same for the rotary potentiometer, search for an example and use that within your own code.

This was my first Arduino project and dispite programming Rockwell 6502 8 bit processors in the 1980s in machine code, I wasn't really a programmer.I did however like the simplicity of the code structure (initialisation, setup and main loop) and the Arduino IDE was very easy to use. Much simpler, in my opinion, than Python for example on a Raspberry Pi.

One piece of advise, never use the delay() function. Everything stops during this time. If you want to turn an LED on for 2 seconds, switch it on, and use a timer within the main loop to switch it off again after 2 seconds.

Good luck!

Thanks for the advice - well appreciated especially delay function - was worried about that myself - not smart programming to use it really when there is a need for quick response (about to crash!). I have the transmitter end all done apart from sending the codes. I've sorted out the detent pot so that it increases turning it clockwise and decreases turning anticlock. When it reaches zero an led lights and pot won't decrease any further. Pressing the centre of the pot changes direction and zeros also lighting direction led. Push buttons will control myloco sound card (my biggest investment!) in the engine. In the 80s/90s I wrote a lot of progs in visual basic and much earlier than that wrote games code for Spectrum! But bit rusty on the c++ code that I think is the basis for Arduino? So I'll plod on, look up the libraries, and let you know when I get communication! Thanks
 
A quick search reveals that most tutorials/examples use the RF24 library. I would therefore recommend that rather than the one I used. Such as:

NRF24L01 Transceiver: Getting Started, Arduino Guide

Also ensure that in the code you only allow a change of direction when the loco is stationary, changing direction at speed is not good for motor and gearbox!

Hi thanks again. The button on the detent pot resets the counter to zero and you have to move the knob to start it - I guess that will be ok?
Yes I read that the sketches for the RL24 are the same as the built in RF NANO version so that will make my search easier! Actually had some work in today so maybe tomorrow! I've been isolated for 4 weeks so got time on my hands!
 
I found the Electronoobs website really helpful

Rik
 
I found the Electronoobs website really helpful

Rik
Thanks for link Rik, still struggling to work out how to send a stream of data. I thought I would construct a string with each bit a push button state - 0 or 1. So for example "010000" would be 6 push buttons and button 2 would be ON and the others OFF. The other 2 bits for my 8 bit string would be the loco speed 0-99. Am I being stupid?
 
Thanks for link Rik, still struggling to work out how to send a stream of data. I thought I would construct a string with each bit a push button state - 0 or 1. So for example "010000" would be 6 push buttons and button 2 would be ON and the others OFF. The other 2 bits for my 8 bit string would be the loco speed 0-99. Am I being stupid?
I don't think you need to make it that complicated. You just need to connect each switch and the pot to an input on the Arduino, read the state of the inputs and then transmit the state -

Eg
sent_data.ch1 = map( analogRead(A0), 0, 1024, 0, 255);
sent_data.ch2 = map( analogRead(A1), 0, 1024, 0, 255);
sent_data.ch3 = map( analogRead(A2), 0, 1024, 0, 255);
sent_data.ch4 = map( analogRead(A3), 0, 1024, 0, 255);
sent_data.ch5 = digitalRead(2);
sent_data.ch6 = digitalRead(3);
sent_data.ch7 = map( analogRead(A4), 0, 1024, 0, 255);

radio.write(&sent_data, sizeof(Data_to_be_sent));

The analog input needs to be remapped from the range 0 to 1024 to 0 to 255 (or just divided by 4) and the switch inputs need to be digitally read and sent.

I copied the code from this link on the Electronics website for a seven channel tx with five pots and two switches. You just need to adjust the analog and digital reads to suit your tx


Rik
 
Micky, there are many ways to format and send the data. For my first system I sent 8 bytes similar to the way Rik describes above, 1 byte for the speed, 1 for direction etc.

In my latest system I send several bytes of data, one of which is a command byte, like speed or direction, followed by the command data.

e.g. 01 NN = 01 for speed command, NN for speed
02 01 = 02 for direction command, 01 = forward
02 02 = 02 for direction command, 02 = reverse
03 NN = 03 for sound command, NN for sound type
etc.

This is more flexible and makes it easier to upgrade. For example, I've just updated my handheld controller to operate my points by adding a new command.

Both systems will work, you have to choose the best one for your current and future needs.
 
Back
Top