Stepper motors are widely used in various applications such as robotics, 3D printing, and automated machinery. These motors are known for their precise positioning and can be controlled in small steps, making them ideal for applications that require accurate motion control. In this article, we will discuss how to wirelessly control a stepper motor with an Arduino and a 433MHz RF module.
What is a Stepper Motor?
A stepper motor is a type of electric motor that uses magnetic fields to control the position of the shaft. The motor moves in small, precise steps, making it ideal for applications that require precise positioning. Unlike DC motors, which rotate continuously, stepper motors can be controlled to rotate in a specific number of steps. Here Nema 17 stepper motor will be used. The tutorial Arduino stepper motor position control potentiometer explains in greater details how to use Arduino to control a stepper motor.
What is a 433MHz RF module?
A 433MHz RF module is a wireless communication module that uses radio frequency technology to transmit and receive data over long distances. These modules are widely used in various applications, such as remote control systems, wireless data transmission, and home automation. Picture below shows the transmitter and receiver RF module.
How to Control a Stepper Motor with an Ardui
no and a 433MHz RF module?
To control a stepper motor with an Arduino and a 433MHz RF module, we will need to use two Arduino boards, one for the transmitter and one for the receiver. The transmitter will send the data to control the stepper motor to the receiver, which will control the motor based on the received data.
Circuit diagram
The following is the circuit diagram for wireless stepper control using 433MHz RF module and two Arduino boards.
The first step is to connect the 433MHz RF module to the Arduino boards. The module should be connected to the digital pins of the Arduino board and the power supply of the module should be connected to the 5V or 3.3V pin of the Arduino.
Transmitter circuit diagram
For the RF transmitter, we are using Arduino Nano which is connected to the 433MHz RF module at pin 12. Note that pin 12 is used because we are using the Radio Head library which uses the pin 12 as the transmit pin. On breadboard it looks like the following.
Receiver Circuit Diagram
The following is the schematic diagram of the receiver.
The above is the circuit diagram of the RF module Arduino receiver. The 433MHz RF module data pin is connected to the pin 11 of the Arduino Uno. The pin 11 is used because we are using the Radio Head library which uses pin 11 as the default receiver pin. The stepper motor is connected to the Arduino via the L298N motor driver IC(Integrated Circuit). The Arduino pins 7,8,9 and 10 are connected to the input pins IN1, IN2, IN3 and IN4 of the L298N motor driver and the Arduino pin 12 is connected to the EN(enable A and B) pins of the L298N. The output pins of the L298N OUT1, OUT2, OUT3 and OUT4 are connected to the 4 wires of the stepper motor via diode bridge as shown in the circuit diagram above.
The following pictures the breadboard assembled receiver circuit.
Programming
Next, we need to install the RH_ASK library, which will be used to communicate between the transmitter and receiver using the 433MHz RF module. The library can be installed from the Arduino library manager. The 433Mhz RF Arduino tutorial explains in greater details how to install the Radio Head library with simple code for wireless data transfer.
For the stepper motor control we will be using another library in the receiver program called the stepper.h.
The next step is to write the code for the transmitter and the receiver. The transmitter code will send the data to control the stepper motor to the receiver. The receiver code will receive the data and control the stepper motor based on the received data.
Transmitter Code:
#include <RH_ASK.h>
#include <SPI.h>
RH_ASK rf_driver;
void setup()
{
Serial.begin(9600);
if (!rf_driver.init())
Serial.println("init failed");
}
void loop()
{
static int counter = 0;
uint8_t data[1];
data[0] = counter;
counter++;
rf_driver.send(data, sizeof(data));
rf_driver.waitPacketSent();
Serial.println("Sending data: " + String(counter));
delay(1000);
}
The transmitter code sends the data to the receiver through the 433MHz RF module. The code starts by including the RH_ASK library and the SPI library. The RH_ASK library is used to communicate between the transmitter and receiver using the 433MHz RF module, while the SPI library is used to communicate with the RF module.
A RH_ASK
object, named rf_driver
, is created to communicate with the RF module. The setup
function initializes the serial communication with a baud rate of 9600 and initializes the rf_driver
object. If the initialization fails, it prints an error message.
The loop
function continuously sends the data to the receiver. A static integer variable counter
is used to store the data that will be sent. The counter
value is incremented in each iteration. The data is stored in an array of size 1 named data
, and its first element is the value of counter
.
The rf_driver.send
function is used to send the data to the receiver, and the rf_driver.waitPacketSent
function is used to wait until the data has been sent. The Serial.println
function is used to print the value of counter
that was sent. The delay
function is used to delay the loop execution by 1000 milliseconds.
Receiver Code:
#include <RH_ASK.h>
#include <Stepper.h>
#include <SPI.h>
#define stepsPerRevolution 200
RH_ASK rf_driver;
Stepper myStepper(stepsPerRevolution, 7, 8, 9, 10);
void setup()
{
Serial.begin(9600);
if (!rf_driver.init())
Serial.println("init failed");
myStepper.setSpeed(60);
}
void loop()
{
uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
uint8_t buflen = sizeof(buf);
if (rf_driver.recv(buf, &buflen))
{
Serial.print("Message received: ");
for (int i = 0; i < buflen; i++)
{
Serial.print(buf[i]);
}
Serial.println();
myStepper.step(buf[0]);
}
}
The receiver code receives the data from the transmitter through the 433MHz RF module and controls the stepper motor. The code starts by including the RH_ASK library, the Stepper library, and the SPI library. The RH_ASK library is used to communicate between the transmitter and receiver using the 433MHz RF module, while the Stepper library is used to control the stepper motor.
A RH_ASK
object, named rf_driver
, is created to communicate with the RF module, and a Stepper
object, named myStepper
, is created to control the stepper motor. The setup
function initializes the serial communication with a baud rate of 9600 and initializes the rf_driver
object. If the initialization fails, it prints an error message. The speed of the stepper motor is set to 60 steps per second using the myStepper.setSpeed
function.
The loop
function continuously listens for the incoming data from the transmitter. A buffer named buf
is used to store the received data, and its size is determined by the RH_ASK_MAX_MESSAGE_LEN
constant. The rf_driver.recv
function is used to receive the data, and the received data is stored in the buf
buffer. The Serial.println
function is used to print the received data.
The myStepper.step
function is used to control the stepper motor, and the number of steps the motor will take is determined by the first element of the buf
buffer.
Video demonstration
The following video demonstrates how the stepper motor is controlled using the 433MHz RF module and the Arduino.
Summary and References
So in this tutorial we explained how one can use the 433MHz RF module to control a stepper motor wirelessly using Arduino. We provided the circuit diagram of interfacing of Arduino stepper motor L298N and provided the program code for the transmitter and receiver to transfer control data over RF communication channel.