Hello Motor

Controlling motion is a primary requirement when it comes to building physical IoT system. Learn how to read values from Analog to digital converters while controllong stepper actions to build your internet controlled machines.  View Source

What you’ll learn

The project consists of an emulated Raspberry Pi connected to digital input/output and a temperature/humidity sensor.

The basics of programming motion

IoT is the system of system, which allows connecting numbers of sensors, actuators, and detectors which all are uniquely addressable and controllable over the network protocols. All the controllable devices in a system fall under the Actuators category.

What is an actuator?

An Actuator can be defined as a device that converts energy (in terms of robotics, that energy tends to be electrical) into physical motion. For instance, Stepper Motor or Hydraulic system.

We have a wide family of controllable devices (Actuators). Most commonly used actuators in applications these days are stepper motor, servo motor, DC motors and AC motors. Internet of Things will help us achieve the next industrial boom by allowing to control these devices remotely and intelligently. The major difference between the Internet of Things and the traditional controller is that the new generation of controllers are smart and connected. Connectivity between the cloud and machines will enable us to combine the data, analyze the events, improve the action and manipulate objects in the real world. Choosing a perfect motor for the mechanical application is always an important task.

The basics: Analog to Digital Converter

An Analog to digital converter devices converts an analog value such as voltage to a digital output (regarding binary digit). A2D converters are essential components for interacting with the physical world and digital microprocessor. While most of the event in the real world always happens on analog domain such as displacement, acceleration, fall, the microprocessor could only understand digital values in binary format. A2D converters help bridge this gap.

The basics: Digital to Analog Converter

This is just reverse of what an A2D converter is, i.e. converting digital values to their physical counterpart. For example, motion of a stepper motor or a voltage controlled relay is the classic example of D2A converter.

In this project, we will focus on 3 elements of robotics :

Let’s get started. Say Hello! To the internet of Motion with IoTIFY.


A stepper motor or a step motor or a stepping motor is a brushless DC motor, which works on a principle of electromagnetism, that rotates into discrete step angles and makes a complete rotation.

DC motor starts rotating just by applying DC power source to the terminals, but in a case of a stepper motor, it runs on a train of pulses (Square Wave pulses). Each pulse is making the shaft move through a fixed angle.

How stepper motor works?

The below animation shows the working of 4 phase unipolar stepper motor.

It is a brushless DC motor and as it is not having any feedback unlike servo motor so, can be the open loop controllable electronic device. It generally consists of the permanent magnet surrounded by a stator.

As you activate the windings step by step in a particular order and let the current flow through them, it magnetizes the stator creating electromagnetic poles respectively and that will cause particular angular rotation.

Let’s get it started with IoTify.

1. Getting Started

Signup with iotify.io if you haven’t yet. The free account will have sufficient credits to run this project. Once you are logged into the IoTIFY main application, go to the virtual lab and click on the Hello Motor project.

Provide a unique name for your project. Then simply click launch.

The project takes 3-5 minutes to launch. The project state will automatically change to running once it is ready. You could then simply click the Open button and it will launch a new window with the Workspace of the project.

The webview has a stepper motor, 4 knobs, few switches, and LEDs.All of these internally connected to an emulated raspberry pi 2, so that you can interact with the corresponding peripheral from the terminal by the programming.

The project contains a stepper motor connected to raspberry pi GPIO via driver IC ( DRV8834 By texas Instruments) The driver IC is connected via GPIOs.

The 4 knobs, Whose output is an analog voltage, are connected to an analog-to-digital converter (ADS1115 texas Instruments), Which is connected to emulated pi via I2C (Inter-Integrated Circuit) as we are not having any analog GPIO in our raspberry Pi. You can read the voltage rating by reading from the ADC via I2C.

The web view contains the LEDs and Switches that are connected directly to the Raspberry pi GPIO.

Physical connection of stepper motor with Raspberry Pi

Now, below fig. Shows the Physical connection of stepper motor with raspberry pi (Here we have used DRV8834 Texas Instruments IC as a stepper motor driver).

2. Reading the analog voltage.

Open a console terminal to get an ssh access. The source code for the project is already downloaded in hello-motor directory. Here we have two python scripts – one for the motor and one for the ADC converter.

Reading analog voltage values from Pi:

The a2d.py script in hello-motor directory continually polls each of the 4 analog input of ADC. Type following command in your console and you can see the analog voltage values.

cd hello-motor
sudo python a2d.py

Now, if you go to the webview and try to move any of the 4 knobs, you should see in the console output the corresponding voltage values are changing.

What is happening here?

The voltage knobs are simulating 4 analog channels and software is reading values of those channels via A2D converter. Those 4 channels could represent any 4 analog values such as tilt values at a drone wings or tyre pressure of 4 tyres. The software continously monitors the analog values and based upon that it takes certain actions.

3. Run the Stepper motor

Let’s move to the stepper motor. Now, run the motor.py python script as root user in the console. It will interact with emulated motor driver IC, which then moves the motor in the webview.

sudo python motor.py

You can see the motor is rotating as we have both red mark on the stepper motor and a numeric value of the rotation angle.

Stepper motor executes rotation in “steps” where a step corresponds to a fixed amount of rotation; In typical embedded applications, we use motors with 200 steps per revolution, Which means that one step corresponds to a 1.8 degree rotation. That’s why we chose our motor to have 200 steps per revolution.

To change the steps, Open the motor.py script and here you can see, it executes a rotation of a fixed number of steps given by “steps” variable.

steps = -10

Of Course, you can change the value of the “steps” variable and re-execute the script and see the motor is rotating by a different amount. Motor will rotate clockwise if the number of steps is positive, and counter-clockwise if the number of steps is negative.

Here, also you can find some declaration at the beginning of the script. It shows which Rpi GPIOs are used to connect different pins of DRV8834 IC (Motor driver IC). So that you can have the idea how Driver IC is connected to the raspberry Pi GPIO. The Driver IC can work in both modes i.e. Indexer Mode and phase/enable mode.

STEP_BENBL = 7        # Bridge B enable
DIR_BPHASE = 8 # Bridge B Phase
M1 = 9 # Disable state
M0_APHASE = 10 # Bridge A Phase
NSLEEP = 11 # Sleep mode input
NFAULT = 14 # Fault output
NENBL_AENBL = 15 # step motor enable
CONFIG = 17 # Device Configuration

The motor.py script is for both the modes, you can change the mode by just changing the “indexer_mode” variable value (True or False).By Default it is in Indexer mode because it’s easier to use. You have the privilege to modify the code and make it work as per your application.

Where to go further

We have shown you the basics of controlling a mechanical system with IoTIFY. Further ideas could be

Do you love this project? Tell us what you think at the discussion forum, or share with your colleagues by clicking social icons below.