Projects

IoT Motor Control

Controlling motors over Wi-Fi

Luke Prior

Issue 56, March 2022

An easy to build Arduino-based home automation project to demonstrate how a motor and similar everyday devices can be controlled in your home via Wi-Fi.

BUILD TIME: A few hours (plus 3D printing time)
DIFFICULTY RATING: Intermediate

This project started as a proof of concept to demonstrate how a motor can be controlled wirelessly in a home using home automation software. This could, for example, be the basis for controlling interior blinds, opening or closing a gate, or turning a dial on an appliance up or down. A volume dial, for instance.

The outcome of this project is the ability to control a small motor over Wi-Fi using open-source home automation software, with a rechargeable battery power source to make it suitable for applications where mains power is not available.

The broad overview

Our build uses an Espressif ESP8266 processor and its integrated Wi-Fi for wireless operation. The ESP8266 interfaces with a TB6612 motor driver to control the speed and direction of the attached motor.

Software controls are handled by ESPHome, which is a configurable firmware for custom IoT devices that enables integration with a Home Assistant installation. This compatibility with Home Assistant allows the device to be integrated with other smart-home devices, allowing for complex automation to be created.

An integrated LiPo battery and charge controller provides seamless switching between wired and wireless operations without downtime.

How it works

Microcontroller

We chose to use an ESP8266 as our microcontroller as it features integrated WiFi and is one of the most widely supported boards available. In our example, we have used the Sparkfun ESP8266 Thing Dev, however, the specific version doesn’t matter for this application because we are just using two output pins.

Motor Driver

To drive our motor, we will be using the Adafruit TB6612 motor driver, which is a modern efficient chip that will serve our purposes well.

This versatile board has two full H-bridges (four half H-Bridges), so it can spin two DC motors bidirectionally, step a bi-polar or uni-polar stepper motor, or fire off two solenoids. It has a 1.2A current limit, two digital inputs per H-bridge (one for each half of the bridge) as well as a PWM input per driver, which provides motor speed control. The board can run from 2.7V to 5V logic, and has a separate power circuit to operate the motors from 4.5V up to 13.5V.

Note: To learn more about H-bridges, check out our Bridging The Gaps project in Issue 12 or Classroom from Issue 31.

Battery Power

We have used the Adafruit PowerBoost 1000 board, which provides a stable 5V output from the 3.7V LiPo battery to power the circuit. This board charges the battery when it is connected to a USB power source, and seamlessly switches between battery and corded power without any power interuption.

Our build uses a compact 350mAh LiPo battery, however, a different capacity battery can be used depending on the battery life your application needs, and the power characteristics of the device you are controlling.

The Motor

We have used a commonly available 5V DC motor to demonstrate the project. This, of course, cab be swapped for a different motor, stepper or solenoid, depending on your application. Keep in mind that the TB6612 board can handle voltages between 4.5V to 13.5V up to 1.2A.

The software

We will be running ESPHome firmware on the ESP8266 to control the motor. The ESPHome supports a wide variety of motor drivers including the ubiquitous L298N.

ESPHome is part of the Home Assistant project (https://www.home-assistant.io/) which is an open-source central control system for smart home devices with a focus on local access and security. It is powered by a worldwide community of makers, and is perfect to run on a Raspberry Pi or a local server. In our example, we will show you how to set it up using a Raspberry Pi.

The Build:

Circuit Assembly

Parts Required:JaycarAltronicsPakronics
1 x ESP8266 or equivalentXC3802Z6381ADA2821
1 x Adafruit TB6612 1.2A DC/Stepper Motor Driver Breakout Board--ADA2448
1 x Adafruit PowerBoost 1000 Charger--ADA2465
1 x 3.7V LiPo Battery ^SB2301S4724ADA4237
1 x 5V DC Motor #YG2900J0026-

Parts Required:

If not factory fitted, you may need to solder the pin headers to the Adafruit TB6612 motor controller so that it can be placed into a breadboard.

The Adafruit PowerBoost 1000 will also need cables soldered to it for connecting with the 3.7V LiPo battery and for powering the ESP8266 and motor.

The motor can also have a pair of wires soldered to the two tabs for connecting with the controller.

Insert the ESP8266 and motor controller boards into
the breadboard.

We used the breadboard power rail to wire two 5V lines from the PowerBoost 1000 to the ESP8266 and motor driver.

The ESP8266 can be powered from the VIN pin, which is connected directly to the internal 3.3V regulator.

The TB6612 will use the 5V input for driving the motor so it can be attached to Vmotor and GND.

While the motor is driven with 5V, the ESP8266 is a 3.3V microcontroller, so the logic level voltage on the TB6612 motor driver must also be 3.3V. This can be achieved by wiring the 3.3V power pin from the ESP8266 to the Vcc pin on the TB6612.

The motor can be wired to the two Motor A pins on the TB6612 and then we can proceed to wiring the three cables required to enable speed and direction control of the motor.

The pins AIN1 and AIN2 on the TB6612 can be wired to pins 5 (D5) and 4 (D4) on the ESP8266 Thing Dev.

The final PWMA pin needs to be wired to the 5V distribution to allow the ESP8266 to control the motor with the previous two wires.

This completes the assembly of the build, so we can begin setting up the ESPHome software required to program and interface with it.

Software Setup

The ESPHome firmware that we will be running on the ESP8266 to control our motor is part of the Home Assistant project and requires an active installation.

We will need an active Home Assistant installation to both program the ESP8266 and then control the motor through its website interface.

The most common installation device is the Raspberry Pi with the model 3 & 4 working the best, however, the software will run on most devices and as a Docker container.

To use Home Assistant with a Raspberry Pi, the ready-made image can be flashed to an SD card and the Raspberry Pi connected to ethernet for internet connectivity. The correct image for your specific Raspberry Pi can be found on the installation page here: https://www.home-assistant.io/installation/raspberrypi

Once Home Assistant has started, you can connect and configure it by navigating to homeassistant.local:8123 or if that does not work, directly to the IP address of the Raspberry Pi in your web browser on port 8123.

This will allow you to configure a user account and be greeted with the default Home Assistant dashboard. At this point, the ESPHome dashboard will need to be installed as an add-on. The software can be found in the add-on store accessible from the Supervisor panel on the main Home Assistant sidebar.

Once ESPHome has been installed on the Home Assistant installation, it can be started and the Web UI accessed. This will greet you to the introduction wizard, which we will use to set the properties for our specific ESP8266 device.

The device name can be anything that you would like and will be used to identify the device in the Home Assistant dashboard. We selected the SparkFun ESP8266 Thing - Dev Board from the specific board menu but if you are using another ESP8266 not listed, you can choose the default ESP8266 option.

You will also need to configure the WiFi network that you want the device to connect to. This should be the same network that the Home Assistant installation is connected to. This will ensure that the two can seamlessly communicate and avoid any networking issues.

Once that is completed, you will be prompted to install the configuration. This can be skipped as further configuration is required to ensure that our motor driver works correctly.

The device YAML file can be edited from the dashboard to add the required parameters.

We can use the ESPHome H-bridge fan component to control our motor as it supports a wide variety of motor drivers, including the TB6612. The two motor driver pins on the ESP8266 will need to be defined as PWM outputs with the following code.

#output:
- platform: esp8266_pwm
id: motor_forward_pin
pin: GPIO5
- platform: esp8266_pwm
id: motor_reverse_pin
pin: GPIO4

The motor entity can then be set up by setting an ID and name for the motor that will appear alongside the control in the Home Assistant interface, along with specifying the pins previously defined as outputs.

fan:
- platform: hbridge
id: my_fan
name: "Test Fan"
pin_a: motor_forward_pin
pin_b: motor_reverse_pin

The configuration file for our ESP8266 and motor is now complete so we can proceed to creating a custom image and flashing it to the device.

Programming

The easiest way to program the ESP8266 with our custom ESPHome image is to connect it directly to the Raspberry Pi or computer running Home Assistant.

The ESP8266 needs to be disconnected from the battery and motor driver, and attached to the Raspberry Pi via
a USB cable.

The device can then be programmed by selecting install on the ESPHome dashboard and choosing ‘Plug into the computer running ESPHome Dashboard’. You should see a single device which will be the ESP8266. Selecting this will begin the installation process by first compiling and then uploading a custom program with our configured parameters to the board.

The status of this operation can be monitored through the logs provided, and after a few minutes, the ESP8266 should connect to the WiFi network previously configured.
The project can now be reassembled with the battery and motor driver.

Testing

The device is now ready to operate from the Home Assistant dashboard. The controls can be accessed from the configuration tab under devices where the configured device should be visible. The page for the device will include an entities section with an option to “ADD TO LOVELACE”, which will place the controls on the main overview page for easy access.

Once the controls have been added to the main dashboard, it is possible to adjust the speed of the motor between 0 and 100, along with setting the direction. The advantage of Home Assistant is that these controls can be used in automation with other smart home devices added to the installation. That is outside the scope of this project but could easily be interfaced with a sensor or online external API.

3D Case

Once we had the prototype working, we decided to design a 3D printed case to securely hold the motor and associated electronics.

We would normally transfer the prototype from the breadboard to a protoboard or a custom PCB but as we were interested in adding extra functionality in the future to customise the device for specific applications, we decided to leave it on the breadboard.

We measured all the components and designed simple friction fit two-part case with a rear port to access the charging and programming connectors. The case features recessed areas for easy component placement.

The case was printed over 10 hours on our Ender-3 Pro 3D printed using PLA filament at 100% infill. This could be optimised further to reduce filament usage and print-time however these weren’t major concerns for this project.

The components could then be secured in the casing using a combination of double-sided tape and Blu Tack but superglue or epoxy could be used for a more permanent solution. We also took the chance to apply some electrical tape to the bottom of the PowerBoost 1000 charge controller to minimise the risk of any electrical shorts.

The case could then be closed and firmly secured using a few electrical tape straps ensuring that the device remains firmly secured regardless of eventual mounting position.

Applications

While we used the included propeller that came with our DC motor for this demonstration, the project can be used in a variety of home applications to help automate tasks. This includes any device that can be interacted with using simple rotational movement. For example, this could include opening/closing of blinds/shutters, adjustment of settings using a rotary dial on a kitchen device or stereo system, and the actuation of push buttons on any device.

The propeller can be replaced with a gearbox and a custom socket to connect with the existing cord on a set of blinds or awnings to automatically configure their position. You could add a sensor to detect the current position of the blinds to provide feedback on the Home Assistant dashboard and ensure that the motor does not get overworked.

To control an existing rotary dial a custom gripper interface can be designed and 3D printed that will connect the dial to the motor output allowing for direct control. This could be used to control the volume on a legacy stereo system that lacks smart connectivity without needing to make any modifications to the device itself which could cause permanent damage.

Where to from here?

The current project works excellently when used as a simple IoT fan, however, if you wanted to use it for one of the potential applications mentioned above, certain upgrades may be beneficial. The current motor can reach a significant speed but has low torque, which would need to be addressed if planning to be used with something like blinds. This could be addressed in two ways; first by introducing a gearbox between the motor and the blinds to reduce the speed, and increasing the torque; and by switching to a more powerful motor. The TB6612 motor driver can drive a maximum of 1.2A, however, both H-bridges could be combined to increase that to 2.4A with a maximum motor voltage of 13.5V.

The power delivery system would need to be redesigned in this case, as the Adafruit PowerBoost 1000 has a maximum output of 1A and the current LiPo battery would not have the capacity to operate off battery power for any sufficient period.

The ESP8266 should be sufficient for operations with various simultaneous sensors and outputs but you may benefit from the newer ESP32 in terms of program storage space and WiFi connectivity. ■

Reading & Resources: