Skip to content

10. Output devices

Group assignment: - Measure the power consumption of an output device

Individual assignment: - Add an output device to a microcontroller board you’ve designed, and program it to do something

Have you answered these questions?

  • Linked to the group assignment page
  • Documented how you determined power consumption of an output device with your group
  • Documented what you learned from interfacing output device(s) to microcontroller and controlling the device(s)
  • Described your design and fabrication process or linked to previous examples.
  • Explained the programming process/es you used.
  • Outlined problems and how you fixed them
  • Included original design files and code
  • Included a ‘hero shot/video’ of your board

What I’ve done this week

Group assignment:

Individual assignment:

  • DRV8825 (Moter Driver)

  • Nema17 (Stepper Moter)

Overview

This week, I have controlled a stepper motor and a servo motor used in Final project

I have controlled the following parts painted purple below.

In the trash sorting part of the final project, the control flow is as follows, and I made the circuit and program.

① Put trash on the table of the sorting machine.

② A camera attached to the Rasbperry Pi recognize trash among (trash that can be turned into charcoal, trash that cannot be turned into charcoal, plastic, recyclable trash, and batteries).

~~~~~ sorting part (OUTPUT) ~~~~~~

③ Rasbperry Pi “GET” request to ESP32 for target trash name via HTTP protocol. (This time, HTTP requests are thrown directly from the browser to ESP32)

④ Esp32, waiting as a web server, receives the “GET” request

⑤ Target trash is moved to the front of the target trash box by the Stepper moter controlled by the ESP32 board

⑥ ESP32 tilts the table of the sorting machine with servo motor.

⑦ Trash falls into the trash box.

⑧ Restore the angle of the table with servo moter.

⑨ Return the table of the sorting machine to its initial position with stteper motor.

⑩ ESP32 returns “success response 200” to RaspberryPi

~~~~~~ sorting part (OUTPUT) ~~~~~~~

DRV8825 (Moter Driver)

I used DRV8825 (Moter Driver) for this assignment.

First, I checked the datasheet.

Pinout

Drv8825 Steppermotor Driver ESP32 Tutrial

Datasheet

Current limiting

I adjusted the value in group work.

Kamakura Group Assignment Week10

Schematic diagram

Nema17 (Stepper Moter)

  • Specifications
    • Its rated voltage is 12V
    • Phase current is 2.2A
    • The Holding torque is equal to 40N.cm
    • One step angle will be of 1.8 Deg.
    • Total steps for each resolution will be 200.
    • 4-wire and 8-inch lead
    • Number of phases are 4
    • Total inductance by each phase will be 2.8 mH
    • The resistance of the coil is 1.5 Ohm per coil.
MODE0 MODE1 MODE2 Microstep Resolution
Low Low Low Full step
High Low Low Half step
Low High Low 1/4 step
High High Low 1/8 step
Low Low High 1/16 step
High Low High 1/32 step
Low High High 1/32 step
High High High 1/32 step

Datasheet

The circuit is as follow

These are the control pins which are used to control the where EN, SLP and RST control the power states and DIR and STEP control the input.

SLP and RST are shorted because they are not used in this project.

M0, M1, and M2 are pins for use when using microstep, but are not used this time

VMOT, GND: This is the stepper motor power supply pins. Connect 8.2-45V external power supply with VMOT and common ground.

The DRV8825 motor driver module also features a FAULT pin. This is used for over current protection. The pin goes in a LOW state disabling the driver due to over current protection. What happens is that the FAULT pin which is shorted with the SLP pin when goes in a LOW state that means that the driver is disabled.

To install the library “Sketch” > “Include Library” > “Manage Libraries”

search by typing “accelstepper” then select Install

The code is as follow.

I added a stepper motor control to the program I created in week08 to run a servo motor on a web server.

#include <WiFi.h>
#include <WebServer.h>
#include <ESP32Servo.h>
#include <AccelStepper.h>

#define SSID "ENV_WIFI"
#define PASS "ENV_PASS"

Servo myservo;

#define motorInterfaceType 1
const int dirPin = 22;
const int stepPin = 23;

AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);

int minUs = 500;
int maxUs = 2400;

WebServer server(80);

void setup(){
  Serial.begin(115200);
  myservo.setPeriodHertz(50);
  myservo.attach(21, minUs, maxUs);
  pinMode(LED_PIN, OUTPUT);

  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);

  connectWiFi();
  server.on("/", handleRoot);
  server.on("/charcol", charcol);
  server.on("/notcharcol", notcharcol);
  server.on("/plastic", plastic);
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
}

void connectWiFi(){
  WiFi.begin(SSID, PASS);
  Serial.println();
  while(WiFi.status() != WL_CONNECTED){
  delay(500);
  Serial.print(".");
}
  Serial.println();
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());
}

void handleRoot(){
  server.send(200, "text/plain", "Hello");
}

void charcol(){
  Serial.println("charcol");

  myStepper.setMaxSpeed(1000);
  myStepper.setAcceleration(50);
  myStepper.setSpeed(200);
  myStepper.moveTo(200);
  myStepper.runToPosition();

  myservo.write(60);
  delay(2000);
  myservo.write(0);

  delay(500);

  myStepper.setMaxSpeed(1000);
  myStepper.setAcceleration(50);
  myStepper.setSpeed(200);
  myStepper.moveTo(0);
  myStepper.runToPosition();

  server.send(200, "text/plain", "charcol");
}

void notcharcol(){
  Serial.println("notcharcol");

  myStepper.setMaxSpeed(1000);
  myStepper.setAcceleration(50);
  myStepper.setSpeed(200);
  myStepper.moveTo(400);
  myStepper.runToPosition();

  myservo.write(60);
  delay(2000);
  myservo.write(0);

  delay(500);

  myStepper.setMaxSpeed(1000);
  myStepper.setAcceleration(50);
  myStepper.setSpeed(200);
  myStepper.moveTo(0);
  myStepper.runToPosition();

  server.send(200, "text/plain", "notcharcol");
} 

void plastic(){
  Serial.println("plastic");

  myStepper.setMaxSpeed(1000);
  myStepper.setAcceleration(50);
  myStepper.setSpeed(200);
  myStepper.moveTo(600);
  myStepper.runToPosition();

  myservo.write(60);
  delay(2000);
  myservo.write(0);

  delay(500);

  myStepper.setMaxSpeed(1000);
  myStepper.setAcceleration(50);
  myStepper.setSpeed(200);
  myStepper.moveTo(0);
  myStepper.runToPosition();

  server.send(200, "text/plain", "plastic");
} 

Define Arduino pins to which DRV8825’s STEP & DIR pins are connected.

const int dirPin = 22;
const int stepPin = 23;

Set motorInterfaceType to 1. (1 means an external stepper driver with Step and Direction pins)

#define motorInterfaceType 1

Next, Create an instance of stepper library called myStepper.

AccelStepper myStepper(motorInterfaceType, stepPin, dirPin);

In the setup function set the maximum speed of the motor to 1000. then set an acceleration factor for the motor to add acceleration and deceleration to the movements of the stepper motor.

Next, set the regular speed of 200 and the number of steps 200 (as NEMA 17 moves 200 steps per revolution).

    myStepper.setMaxSpeed(1000);
    myStepper.setAcceleration(50);
    myStepper.setSpeed(200);
    myStepper.moveTo(200);

----------- Update (2022/05/01) -----------

Created board for trash separator for use in Final Project

Wired as follows

The electronic parts are as follows

The board is as follows

The wiring is as follows

These are the control pins which are used to control the where EN, SLP and RST control the power states and DIR and STEP control the input.

SLP and RST are shorted because they are not used in this project.

M0, M1, and M2 are pins for use when using microstep, but are not used this time

VMOT, GND: This is the stepper motor power supply pins. Connect 8.2-45V external power supply with VMOT and common ground.

The following program is used.

The changes are the addition of one stepping motor and three servo motors.

#include <WiFi.h>
#include <WebServer.h>
#include <ESP32Servo.h>
#include <AccelStepper.h>

#define SSID "Kuriyama-DIY-G"
#define PASS "kuriyama"

Servo myservo1;
Servo myservo2;
Servo myservo3;
Servo myservo4;

#define motorInterfaceType 1

const int dirPin1 = 22;
const int stepPin1 = 23;
const int stepSpeed = 1700;
const int stepsPerRevolution = 200;

AccelStepper myStepper1(motorInterfaceType, stepPin1, dirPin1);

const int dirPin2 = 19;
const int stepPin2 = 21;

AccelStepper myStepper2(motorInterfaceType, stepPin2, dirPin2);


int minUs = 500;
int maxUs = 2400;

WebServer server(80);

void setup(){
  Serial.begin(115200);
  myservo1.setPeriodHertz(50);
  myservo1.attach(5, minUs, maxUs);

  myservo2.setPeriodHertz(50);
  myservo2.attach(17, minUs, maxUs);

  myservo3.setPeriodHertz(50);
  myservo3.attach(16, minUs, maxUs);

  myservo4.setPeriodHertz(50);
  myservo4.attach(4, minUs, maxUs);

  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);

  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);

  connectWiFi();
  server.on("/", handleRoot);
  server.on("/plastic", plastic);
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
}

void connectWiFi(){
  WiFi.begin(SSID, PASS);
  Serial.println();
  while(WiFi.status() != WL_CONNECTED){
  delay(500);
  Serial.print(".");
}
  Serial.println();
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());
}

void handleRoot(){
  server.send(200, "text/plain", "Hello");
}

void plastic(){
  Serial.println("plastic");

  myStepper1.setMaxSpeed(1000);
  myStepper1.setAcceleration(50);
  myStepper1.setSpeed(200);
  myStepper1.moveTo(3000);
  myStepper1.runToPosition();

  myStepper2.setMaxSpeed(1000);
  myStepper2.setAcceleration(50);
  myStepper2.setSpeed(200);
  myStepper2.moveTo(3000);
  myStepper2.runToPosition();

  myservo1.write(0);
  delay(2000);
  myservo1.write(180);
  delay(2000);

  myservo2.write(0);
  delay(2000);
  myservo2.write(180);
  delay(2000);

  myservo3.write(0);
  delay(2000);
  myservo3.write(180);
  delay(2000);

  myservo4.write(0);
  delay(2000);
  myservo4.write(180);

  delay(2000);

  myStepper2.setMaxSpeed(1000);
  myStepper2.setAcceleration(50);
  myStepper2.setSpeed(200);
  myStepper2.moveTo(0);
  myStepper2.runToPosition();

  myStepper1.setMaxSpeed(1000);
  myStepper1.setAcceleration(50);
  myStepper1.setSpeed(200);
  myStepper1.moveTo(0);
  myStepper1.runToPosition();

  server.send(200, "text/plain", "plastic");
} 

What I learned

  • This week I was able to learn about stepper motor in depth.

  • To use a stepper motor, basically need a motor driver, which is more difficult to handle than a servo motor or DC motor, but if I can master it, use it to create complex mechanisms.

  • I would like to continue learning stepper motor because the conveyor belt for my final project will also be equipped.

Appendix


Last update: May 2, 2022