🔬
Wiki.Linhkientot
  • 👋Chào mừng tới Linhkientot's Wiki
  • Hướng dẫn phần mềm
    • Cài đặt Arduino IDE 2.0
    • Cài đặt phần mềm Mixly
    • Cài đặt phần mềm Mind+
    • Cài đặt phần mềm Thonny
  • Arduino Products
    • Hướng dẫn ban đầu Arduino IDE
      • Hướng dẫn nạp chương trình Arduino IDE
      • Thêm thư viện Arduino IDE
      • Cài đặt Driver CH340 trên MAC OS
      • Lập trình Board ESP32 trên Arduino IDE
    • Arduino Starter Kit
      • Lesson 0. Getting Started - install IDE
      • Lesson 2. LEDs
      • Lesson 3. RGB LEDs
      • Lesson 4. Eight LEDs and a Shift Register
      • Lesson 5. The Serial Monitor
      • Lesson 6. Digital Inputs
      • Lesson 7. Make an RGB LED Fader
      • Lesson 8. Analog Inputs
      • Lesson 9. Sensing Light
      • Lesson 10 Making Sounds
      • Lesson 11 LCD Displays Part 1
      • Lesson 12 LCD Displays Part 2
      • Lesson 13 DC Motors
      • Lesson 14 Servo Motors
      • Lesson 15 DC Motor Reversing
      • Lesson 16 Stepper Motors
      • Lesson 17 Email Sending Movement Detector
    • Arm Robot
      • Hướng dẫn lắp ráp Arm-4DoF
      • Hướng dẫn lắp ráp Arm-6DoF
      • Hướng dẫn lắp ráp Arm - 6DoF đế tròn quay
    • Car Robot
      • Hướng dẫn lắp ráp tank TS-100
    • Arm Car Robot
    • Smart home IoT
    • Lập trình cảm biến khí ga/ khói MQ-2 Arduino
  • Micro:bit Products
    • Micro:bit Setup
    • Micro:bit Starter Kit V1
      • Setting up the micro:bit with Makecode
      • Set up Arduino IDE for micro:bit
      • Using the Buttons and LED Matrix on micro:bit
      • Pushbutton with micro:bit
      • Tilt Sensor with micro:bit
      • Temperature Sensor with micro:bit
      • DC Motor with micro:bit
      • Sound Sensor with micro:bit
      • Raindrop Sensor with micro:bit
      • Make an RGB LED Blink with micro:bit
      • Ultrasonic Distance Sensor with micro:bit
      • Analog Inputs and micro:bit
      • Servo with micro:bit
      • Smoke sensor with micro:bit
      • Light-dependent Resistor with micro:bit
      • Infrared Obstacle Avoidance Sensor with micro:bit
      • Using LEDs with micro:bit
    • Micro:bit Starter Kit V2
      • Microbit Introduction
      • Microbit Basic Lessons
      • 1. Lesson: Đèn LED nhấp nháy
      • 2. Lesson: RGB Led
      • 3. Lesson: Đọc giá trị từ triết áp
      • 4. Lesson: Đọc nút nhấn
      • 5. Lesson: Servo Motor
      • 6. Lesson: Passive Buzzer-Còi thụ động
      • 7. Lesson: Active Buzzer-Còi chủ động
      • 8. Lesson: Compass-La bàn
      • 9. Lesson: Accelerometer-Gia tốc kế
      • 10. Lesson: Module phát hiện âm thanh
      • 11. Lesson: Cảm biến ánh sáng (quang trở)
      • 12. Lesson: Cảm biến ngọn lửa
      • 13. Lesson: Cảm biến khói
      • 14. Lesson: Cảm biến siêu âm
      • 15. Lesson: ModuleRelay
      • 16. Lesson: Cảm biến nhiệt độ (DHT11)
      • 17. Lesson: Bộ điều khiển từ xa (IR)
      • 18. Lesson: Hiển thị màn hình LCD i2c 1602
      • 19. Lesson: Hiển thị nhiệt độ, độ ẩm trên màn hình LCD
      • 20. Lesson: Điều khiển quạt bằng Relay
    • Micro:bit Advanced Kit V2
      • Giới thiệu BBC Sensor Shield V2
      • Danh sách linh kiện bộ Kit
    • Micro:bit Car
  • ESP32 PRODUCTS
    • Hướng dẫn lập trình ESP32
  • Raspberry Products
    • Raspberry Pico Kit
    • Raspberry Pico Car
  • Group Robots
    • 🤖Arduino Robots
    • 🐦Micro:bit Robots
    • 🍓Raspberry Robots
Powered by GitBook
On this page
  • Arduino Lesson 16. Stepper Motors
  • Overview
  • Parts
  • Breadboard Layout
  • Arduino Code
  • Stepper Motors
  • Other Things to Do
  1. Arduino Products
  2. Arduino Starter Kit

Lesson 16 Stepper Motors

PreviousLesson 15 DC Motor ReversingNextLesson 17 Email Sending Movement Detector

Last updated 1 year ago

Arduino Lesson 16. Stepper Motors

Created by Simon Monk

Last updated on 2022-12-01 01:53:11 PM EST

Table of Contents

Overview 3

Parts 3

  • Part

  • Qty

Breadboard Layout 8

Arduino Code 9

Stepper Motors 11

Other Things to Do 12

Overview

Stepper motors fall somewhere in between a regular DC motor and a servo motor. They have the advantage that they can be positioned accurately, moved forward or backwards one 'step' at a time, but they can also rotate continuously.

In this lesson you will learn how to control a stepper motor using your Arduino and the same L293D motor control chip that you used with the DC motor in lesson 15.

Parts

To build the project described in this lesson, you will need the following parts.

5V Stepper Motor 1

Half-size Breadboard 1

Arduino Uno R3 1

Jumper wire pack

1

Breadboard Layout

The stepper motor has five leads, and we will be using both halves of the L293D this time. This means that there are a lot of connections to make on the breadboard.

The motor has a 5-way socket on the end. Push jumper wires into the sockets to allow the motor to be connected to the breadboard.

Note that the red lead of the Stepper motor is not connected to anything.

Arduino Code

The following sketch uses the Serial Monitor, so once the sketch is installed and running, open the Serial Monitor and enter a number of 'steps'. Try a value of about 500, this should cause the motor to turn through about 360 degrees. Enter -500 and it will turn back in the reverse direction.

The Stepper library is included in newer distributions of the Arduino IDE - you may need to upgrade.

/*

Adafruit Arduino - Lesson 16. Stepper

*/

#include <Stepper.h> int in1Pin = 12;

int in2Pin = 11; int in3Pin = 10; int in4Pin = 9;

Stepper motor(512, in1Pin, in2Pin, in3Pin, in4Pin); void setup()

{

pinMode(in1Pin, OUTPUT);

pinMode(in2Pin, OUTPUT); pinMode(in3Pin, OUTPUT); pinMode(in4Pin, OUTPUT);

// this line is for Leonardo's, it delays the serial interface

// until the terminal window is opened while (!Serial);

Serial.begin(9600); motor.setSpeed(20);

}

void loop()

{

if (Serial.available())

{

int steps = Serial.parseInt(); motor.step(steps);

}

}

As you might expect, there is an Arduino library to support stepper motors. This makes the process of using a motor very easy.

After including the 'Stepper' library, the four control pins 'in1' to 'in4' are defined.

To tell the Arduino Stepper library which pins are connected to the motor controller, the following command is used:

Stepper motor(768, in1Pin, in2Pin, in3Pin, in4Pin);

The first parameter is the number of 'steps' that the motor will take to complete one revolution. The motor can be moved by one step at a time, for very fine positioning.

Serial communications is then started, so that the Arduino is ready to receive commands from the Serial Monitor.

Finally the following command sets the speed that we wish the stepper motor to move, when we subsequently tell it how many steps to rotate.

motor.setSpeed(10);

The 'loop' function is very simple. It waits for a command to come in from the Serial Monitor and converts the text of the number sent into an int using 'parseInt'. It then instructs the motor to turn that number of steps.

Stepper Motors

Stepper motors use a cogged wheel and electro magnets to nudge the wheel round a 'step' at a time.

By energizing the coils in the right order, the motor is driven round. The number of steps that the stepper motor has in a 360 degree rotation is actually the number of teeth on the cog.

The motor we are using has 48 steps, but then the motor also incorporates a reduction gearbox of 1:16 that means that it needs 16 x 48 = 768 steps.

In this lesson, we do not use the common Red connection. This connection is only provided if you are using a different type of drive circuit that does not allow the current in each coil to be reversed. Having a center connection to each coil means that you can either energise the left or right side of the coil, and get the effect of reversing the current flow without having to use a circuit that can reverse the current.

Since we are using a L293D that is very good at reversing the current, we do not need this common connection, we can supply current in either direction to the whole of each of the coils.

Other Things to Do

Try changing the command that sets the speed of the stepper motor:

motor.setSpeed(20);

to a lower value (say 5) upload the sketch and notice that the stepper turns more slowly.

Now try and find the maximum speed for the stepper by increasing the speed above

20. After a certain point, you will find that the motor does not move at all. This is because it just cannot keep up with the stream of pulses asking it to step.

Try disconnecting the orange and pink leads of the stepper. It should still turn, but you will notice that it is weaker, as it does not have both coils working to push the motor around.

Part Qty

L293D IC 1