Skip to content

Chapter 11: Actuators and Motors

While sensors enable Arduino to sense the environment, actuators allow it to take action. Motors, as a subset of actuators, are often used to give motion to Arduino projects. In this chapter, we'll delve into the world of actuators, focusing primarily on motors and how to control them.

Introduction to Actuators

Actuators are devices that convert electrical energy into some form of useful mechanical motion. They are the "doers" in your system, creating reactions based on your Arduino's decisions. Common actuators include motors, solenoids, and relays.

Controlling a Servo Motor

Servo motors are used where precise control of angular or linear position is needed. They have a limited rotation angle, typically between 0° and 180°.

1. Connection:
Signal (servo) → Digital Pin (Arduino)
Vcc (servo) → 5V (Arduino)
GND (servo) → GND (Arduino)

2. Code Snippet:

Firstly, include the Servo library that comes built-in with the Arduino IDE.

#include <Servo.h>

Servo myServo;          // Create a servo object
int servoPin = 9;       // Signal pin

void setup() {
  myServo.attach(servoPin);   // Attach the servo on pin 9
}

void loop() {
  for (int pos = 0; pos <= 180; pos += 1) {
    myServo.write(pos);      // Move the servo to position 'pos'
    delay(15);
  }
  for (int pos = 180; pos >= 0; pos -= 1) {
    myServo.write(pos);      // Move the servo back
    delay(15);
  }
}

Driving DC and Stepper Motors with Motor Shields

1. DC Motors:
These are simple motors that run when power is applied. Their direction can be reversed by switching the polarity.

2. Stepper Motors:
These motors can move to and hold specific angles. They are used in applications where precision movement is necessary.

Driving either motor directly from an Arduino is discouraged due to power and current limitations. Instead, we use motor shields or driver boards.

Using the Adafruit Motor Shield v2:

  • Connection: Mount the shield on top of the Arduino board. Connect the motors to the shield's terminals.

  • Code Snippet for DC Motor:

#include <Wire.h>
#include <Adafruit_MotorShield.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor = AFMS.getMotor(1);  // Get motor port 1

void setup() {
  AFMS.begin();   // Start the shield
  myMotor->setSpeed(150);   // Set speed (0-255)
}

void loop() {
  myMotor->run(FORWARD);    // Run forward
  delay(1000);
  myMotor->run(BACKWARD);   // Run backward
  delay(1000);
}
  • Code Snippet for Stepper Motor:
#include <Wire.h>
#include <Adafruit_MotorShield.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myStepper = AFMS.getStepper(200, 1);  // 200 steps/rev, motor port 1

void setup() {
  AFMS.begin();  // Start the shield
}

void loop() {
  myStepper->step(100, FORWARD, INTERLEAVE);   // Step forward 100 steps
  delay(1000);
  myStepper->step(100, BACKWARD, INTERLEAVE);  // Step backward 100 steps
  delay(1000);
}

Conclusion

Actuators and motors bridge the gap between the virtual commands of Arduino and the physical realm. Whether you're building robots, automated systems, or interactive art, motors give life to your creations. Mastering motor control is indispensable for any Arduino enthusiast, laying the foundation for dynamic, moving projects.

Previous article Chapter 12: Connecting to the Internet
Next article Chapter 10: Interfacing with Sensors

Leave a comment

Comments must be approved before appearing

* Required fields