ESP32: DIY Motor Driver With ESP32 Controller

by TechMartian in Circuits > Microcontrollers

18381 Views, 42 Favorites, 0 Comments

ESP32: DIY Motor Driver With ESP32 Controller

WMOXsFpcS32R2tNPvcRHGw_thumb_75e.jpg
DC Motor Driver with ESP32

This is the simplest motor driver that could possibly be built using only one NPN transistor, which is controlled and driven by the ESP32 micro controller board.

Materials and Tools

lc6KUZ6NQLmWwAOBrtacgg_thumb_757.jpg
  • ESP32 Microcontroller
  • DC motor
  • NPN Transistor -- BC337
  • 100Ω resistor
  • Diode -- N4148 General Purpose
  • Breadboard wires
  • Breadboard'
  • 2x jumper wire

Connecting the Signal

vQomk3pyQQmhTBsGCj%+NA_thumb_758.jpg

Connect a 100Ω resistor to the base (middle) pin of the NPN transistor to protect the EPS32 board from overvoltage, then connect it in series to D5

Connecting the Power Supply

WP5gXyLaQn+oVn8r84n2OQ_thumb_759.jpg
nwoKjYsNTt6G2vyW0EI3gg_thumb_75a.jpg

  • Connect the collector (right) pin to ground.
  • Connect the emitter (left) pin to the positive pin of the general purpose diode.
  • Connect the negative pin of the general purpose diode to 3.3V.

Connect the DC Motor

SG7nXBrbSyWGvXGr2H2OeQ_thumb_75b.jpg

  • Connect either one of the pins to either end of the diode.

Order does not matter, it only changes rotation which is arbitrary and can be coded as active high or active low.

Coding

Screen Shot 2017-08-18 at 11.14.17 PM.png

Choose ESP32 as the board under tools, and plug it in. Choose the corresponding USB port that is labeled ESP32, then upload the following code to the board.

const int motorPin = 5;
void setup()
{
  //set motorPin as OUTPUT
  pinMode(motorPin, OUTPUT);
}
void loop()
{
   motorOnThenOff();
}
// This function turns the motor on and off like the blinking LED.
// Try different values to affect the timing.
void motorOnThenOff()
{
  // milliseconds to turn the motor on
  int onTime = 3000;
  // milliseconds to turn the motor off
  int offTime = 3000; 
  // turn the motor on (full speed)
  digitalWrite(motorPin, HIGH); 
  // delay for onTime milliseconds
  delay(onTime);     
  // turn the motor off
  digitalWrite(motorPin, LOW);  
  // delay for offTime milliseconds
  delay(offTime);               
}