Arduino: wireless contol of 2 stepper motors from Bajdi on Vimeo.
A month ago I wrote a sketch to control 2 little stepper motors with an analog joystick. After getting the Nrf24L01 modules to work last week I wanted to connect the joystick to one Arduino and the stepper motors to another Arduino. It was easier then I thought, I just put the 2 analog values from the joystick in an array. Then we can sent that array to the other Arduino and that’s about it 🙂 I connected the little 28YBJ-48 stepper motors to my Arduino Mega 2560 and the joystick to my Uno. As you can see in the video I’m using 2 different Nrf24L01 modules, the one connected to the Mega 2560 has a preamp and amp the one connected to the Uno is the small version with integrated antenna. I have 2 of each of these modules, but I seemed to have blown one up. I mistakenly connected 5V to one of the small modules and it’s dead 🙁 Already ordered a couple of new ones on Ebay 🙂 Luckily it was only a small module, I paid 20$ a piece for the big modules with preamp and amp, I’m NOT going to blow those up.
Here is the code I used:
Transmitter code:
<pre>// http://www.bajdi.com // Nrf24L01 connected to Arduino Uno // Nrf24L01 connection details http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo // Transmit analog values from joystick to the receiver #include <SPI.h> #include <Mirf.h> #include <nRF24L01.h> #include <MirfHardwareSpiDriver.h> int joystick[2]; void setup(){ Serial.begin(9600); Mirf.spi = &MirfHardwareSpi; Mirf.init(); Mirf.setRADDR((byte *)"clie1"); Mirf.payload = sizeof(joystick); Mirf.config(); } void loop(){ joystick[0] = analogRead(A0); joystick[1] = analogRead(A1); Serial.println(joystick[0]); Serial.println(joystick[1]); Mirf.setTADDR((byte *)"serv1"); Mirf.send((byte *) &joystick); while(Mirf.isSending()){ } }
Receiver code:
// http://www.bajdi.com // Nrf24L01 connected to Arduino Mega 2560 // Nrf24L01 connection details http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo // Receives analog values (joystick) from transmitter and moves stepper motors left/right forward/backward #include <SPI.h> #include <Mirf.h> #include <nRF24L01.h> #include <MirfHardwareSpiDriver.h> #include <Stepper.h> const int stepsPerRevolution = 64; Stepper small_stepper(stepsPerRevolution, 8,10,9,11); Stepper small_stepper2(stepsPerRevolution, 4,6,5,7); int joystick[2]; void setup(){ small_stepper.setSpeed(250); // set first stepper speed small_stepper2.setSpeed(250); // set second stepper speed Mirf.cePin = 48; //ce pin on Mega 2560 Mirf.csnPin = 49; //csn pin on Mega 2560 Mirf.spi = &MirfHardwareSpi; Mirf.init(); Mirf.setRADDR((byte *)"serv1"); Mirf.payload = sizeof(joystick); Mirf.config(); } void loop(){ while(!Mirf.dataReady()){ } Mirf.getData((byte *) &joystick); if (joystick[0] < 490) { small_stepper.step(1); } // step forward if (joystick[0] > 540) { small_stepper.step(-1); } // step backward if (joystick[1] < 490) { small_stepper2.step(1); } // step left if (joystick[1] > 540) { small_stepper2.step(-1); } // step right }
[…] 5 I connected my analog joystick and a Nrf24L01 module to my Arduino Uno. I then uploaded the same sketch that I used to remote control my 2 stepper motors. This sketch transmits the analog values from the […]
I’m very interested in this and how you got it working. I am wondering, would an Arduino Pro Mini work (have enough pins) to support the joystick and the Nrf24L01 module. I’m looking to make a wireless remote to control 3 motors. Thanks!
A pro mini uses the same micro controller (ATmega328) as the Arduino Uno. So no problem.
Thanks to your code I made a wireless link to send MPU data from a HMD device to a RC car.
Thanks a lot!
Hi, Good Job Man. Please, can you post the connection diagram for this project? I am becoming mad to find the right connection thru the code but i have encountered some problems. Thank You so much 😉
Sorry I don’t draw connection diagrams of such simple stuff.
Hi, i am sorry but a am a newbie and my problem is that in your sketch you use the pins 8,9,10,11 and the 4,5,6,7 to connect the ULN2003 but my problem is that, with the reference of the NRF24L01 info in your sketch, for the connection to Arduino Uno using the Mirf Library, some pins are the same as it uses 8,7,11,12,13 so my problem is this.
I hope to receive some kind of help, thanks in advance
That’s because I used an Arduino Mega. Just change the pin definitions to suit your board.
Thank you for your answer. I Know, you’re using Arduino mega and i have changed the CE and CS pins according the arduino uno’s so you are telling that i can change the coils’s pins using the free pins on my arduino uno? (in my case 1,2,3,4 on the first motor and 5,6,9,10 for the second motor). Thank you again for your help 😉
Yes you can change those pins.
Hi man, thank you again for your answer You’re Great 😉
hi great work just the info I was after can you advise me please on how can I make the speed of the stepper increase the more I push the joystick forward thanks.
Make the value in the setSpeed function a variable that changes with the joystick.
Hi, i tried to convert your sketch to be used with 2 arduino uno instead of the mega you are using, changing the pins of the CS and CE pins according to the Mirf’s Library pins for the arduino uno as you suggested answering to my previous answer but nothing works. If you want i can post the modified code i wrote so you could help me to exit from this black hole.
Thank you in advance :=)
This is not the place to post code … I have not used the mirf library in years, this post is OLD. I use a different (RF24) library now.
Hi, sorry to have bothered you, it was just because in the description on your code i saw you have used the Mirf Library. Thank you
Thank you so much, I have been lookin for a site I can learn from. You do great work, I appreciate all the effort you put into it!