Wireless Servo Control

Published

Introduction: Wireless Servo Control

This project controls the rotation of a servo wirelessly by means of a potentiometer (knob). The rotation is constrained to 180 degrees.

Step 1: Components

This project consists of

  • 2 Arduino UNO controller boards with USB connector cable
  • 2 nRF24L01 – 2.4GHz RF transceiver modules
    (for help with these modules refer to http://randomnerdtutorials.com/nrf24l01-2-4ghz-rf...
  • 2 socket adapter boards (backpack chips) for the nRF24L01
  • 1 optional Arduino Compatible 328 ProtoShield Prototype Expansion Board
  • 1 servo
  • 1 analog potentiometer
  • soldering iron and solder
  • wire
  • needle nosed pliers
  • insulating wrap, I used electrical tape

Step 2: Server Board

The server board consists of a transceiver module, the shield board (which connects directly to the Arduino board only one way), and the servo. I decided to include the shield board to avoid the clumsy breadboard and give the project and overall neater finish.

The code and web resource included in the components list detail the transceiver module connections. I decided to solder the connections instead of using temporary connections as in previous projects. Since I'm a beginner, I insulated each solder joint with electrical tape (they weren't pretty).

The shield board pins correspond directly to the Arduino pins. Before attaching the shield board, I connected the ground and 5volt pins to the board rails with wire and solder. I also soldered the components' 5volt and ground wires to the shield board rails, then finally attached the Arduino to the shield board.

The servo is attached to the 3volt pin for power and digital pin 2 for communication.

** Note: only after completing this build did I notice that my Arduino boards are not identical. My server transceiver is powered by the 5volt rail on the shield board, while the client transceiver is powered by the 3volt pin, though I have been led to believe that a function of the adapter chip on the transceiver is to provide the proper voltage. All I can say for sure is that the code provided matched with the configuration shown in the images produces the described effect.

Step 3: Server Coder: Copy and Paste

<p>//SERVER CODE<br>/*
NRF24L01      Arduino
CE       >     D8
CSN      >     D10
SCK      >     D13
MO       >     D11
MI       >     D12
RO       >     Not used 
GND      >     GND
VCC      >     5V
 */
//transceiver wiring</p><p>#include <servotimer2.h>
// servo library</servotimer2.h></p><p>#include <rh_nrf24.h>
// transceiver library</rh_nrf24.h></p><p>#define Servopin  2
//declaration servo output pin</p><p>ServoTimer2 serv;
//declaration of servo name </p><p>RH_NRF24 nrf24;
//declaration of transceiver name </p><p>int timeOUT = 0;
//variable for servo</p><p>int pulses = 90; 
//variable to store pulses</p><p>void setup()
{
  serv.attach(Servopin); //servo stuff</p><p>  Serial.begin(9600); //transceiver stuff</p><p>  if (!nrf24.init())
    Serial.println("init failed"); //serial monitor stuff
  if (!nrf24.setChannel(12))           //set channel to 125
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed"); //serial monitor stuff
}</p><p>void loop()
{  
  
   
  if (nrf24.available())
  {
    uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];   
    uint8_t len = sizeof(buf);
    if (nrf24.recv(buf, &len))             //serial monitor stuff
    {
      Serial.print("got request: ");
      pulses = strtol((const char*)buf, NULL, 10); //data type change stuff</p><p>int prin = map(pulses, 750, 2250, 0, 180); //data type change stuff
Serial.println(prin);
serv.write(pulses);    //makes servo move
    }
  }    </p>}<br>

Step 4: Client Board

The client board consists of a transceiver module and the potentiometer. The transceiver module is wired the same way** as the server board with the exception that without the shield board, it is wired directly into the Arduino board pins.

The potentiometer takes 5v, ground, and is connceted to analog pin 2.

**Note: as mentioned in the server board step, my Arduino boards are not identical. In this case the transceiver is wired to the pin labeled 3.3V, directly adjacent to the 5V pin, but again, everything seems to work fine.

Step 5: Client Code: Copy and Paste

<p>//CLIENT CODE<br>/*
  NRF24L01      Arduino
  CE       >     D8
  CSN      >     D10
  SCK      >     D13
  MO       >     D11
  MI       >     D12
  RO       >     Not used
  GND      >     GND
  VCC      >     5V
*/
//transceiver wiring</p><p>#include <rh_nrf24.h>
//transceiver library</rh_nrf24.h></p><p>int potpin = A2; //potentiometer delaration
int val;</p><p>char tempChar[5];
String valString = ""; //data type change stuff</p><p>RH_NRF24 nrf24; //transceiver stuff</p><p>void setup()
{
  Serial.begin(9600);
  if (!nrf24.init())
    Serial.println("init failed");
  // Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
  if (!nrf24.setChannel(12))
    Serial.println("setChannel failed");
  if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
    Serial.println("setRF failed");
}
 //transceiver stuff</p><p>void loop() {</p><p>  val = analogRead(potpin); //potentiometer stuff</p><p>  val = map(val, 0, 1023, 750, 2250);
  valString = val;
  String str = (valString);
  str.toCharArray(tempChar, 5); //datatype change stuff
  
  nrf24.send(tempChar, sizeof(tempChar));</p><p>}</p>

Step 6: A Note About the Code:

The Code contains some limited troubleshooting functionality in the form of feedback from the serial monitor in the Arduino software interface. When viewing the serial monitor from the SERVER code (ctrl + shift + M), you should be able to see the state of the potentiometer in the form of a number between 1 and 180.

Also, here is the library for the wireless and the servo:

http://www.airspayce.com/mikem/arduino/RadioHead/

https://github.com/nabontra/ServoTimer2

Share

    Recommendations

    • Creative Misuse Contest

      Creative Misuse Contest
    • Clocks Contest

      Clocks Contest
    • Oil Contest

      Oil Contest

    Discussions