element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Azure Sphere Starter Kit
  • Products
  • Dev Tools
  • Avnet & Tria Boards Community
  • Azure Sphere Starter Kit
  • More
  • Cancel
Azure Sphere Starter Kit
Blog Controlling a 28-BYJ48(ULN2003  Driver) stepper motor with Azure Sphere MT3620 Starter Kit
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Azure Sphere Starter Kit to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: sleepycrat
  • Date Created: 15 Aug 2019 5:30 AM Date Created
  • Views 1784 views
  • Likes 6 likes
  • Comments 7 comments
  • uln2003
  • 28byj-48
  • azurespherech
  • azure sphere
  • azure sphere mt3620 starter kit
  • azuresphkt
  • stepper motor
  • azure sphere starter kit
  • iot
  • sensing the world
Related
Recommended

Controlling a 28-BYJ48(ULN2003  Driver) stepper motor with Azure Sphere MT3620 Starter Kit

sleepycrat
sleepycrat
15 Aug 2019

Preface

I am planning on creating a project for the Azure Sphere contest. I have narrowed down the ideas I want to try. Nevertheless most of them will share some common functionality throughout. Since big projects are made up just figuring out little things, I have decided to blog about my process as I approach these problems and what I learn from them. So the first little thing I wanted to tackle was motion. I will probably need to move something else using a motor of some kind, so I spent a weekend tinkering around and figured out how to get a stepper motor moving by command. I seen other guides tackle this too, but I needed to go through the process myself and make sure I understood what everything was doing.

 

Introduction

This blog will be a post about how to control a basic motor with the Azure Sphere MT3620 Starter Kit module. Using other MCU modules such as an Arduino can make the process of controlling a stepper motor a trivial thing due to the large amount of documentation on the subject and also because of the large amount of supporting libraries that are easily available. The "newness" of the Azure means the there are fewer things documented and that the differences in the architecture of the device means that we have to do some tweaking to get the things we want.

 

image

 

The Stepper motor

The motor we will be using is the 28-BYJ48. It is a commonly found motor in different electronics, machines, and toys. It is also very cheap and from what I can tell very easy to find online.

image

The science behind the stepper motor is very straight forward. Internally, the central shaft is surrounded in a circular pattern with electromagnets. Periodically the electromagnets are sent an electric charge one after another. They are sent these charges in a sequence known as "steps" This allows the shaft to rotate in very precise fixed positions. The motor we have has a set of four electromagnetic coils. According to the documentation the motor will move a specific distance every step. To complete we a full rotation we implement that number of steps which is (360 / distance in degrees). The style of motor will have a lower speed than a DC motor but will have higher levels of torque and be able to make movement more precisely. The motor also includes 5 wires, one for power and the others assigned to igniting a coil

 

I don't do the explanation that much justice but here is an excellent video by the Youtube channel DroneBot WorkShop explaining the mechanics of stepper motors and also in depth coding of the device using an Arduino. We will use the approach this code takes to do something similar with our Azure Sphere.

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

The Motor Driver

We also will use the ULN2003 motor driver. This is a very simple and inexpensive driver that allows us to interface with stepper motor by handling things like supplying power, allowing us a clean way to send signals to the motor, and even giving us visual aid LEDs that light up when we successfully signal to the corresponding coil.

image

 

We're ready. Let's Go. The Prerequisites.

The things you will need for this tutorial are the following

  • An Azure Sphere MT3620 Starter Kit. I got mine for the contest. You can pick one up here Azure Sphere
  • A 28-BYJ48 stepper motor.
  • A ULN2003 motor driver.
  • Jumper wires to connect everything.
  • A 5V\500mA power supply. I used my bench power supply for this, but you can use whatever

 

Here's a link to a kit of 5 motors and 5 drivers and some jumper wires here. Stepper motor kit

 

The setup

First and foremost you should have set up your Azure Sphere. Follow the Instructions here Getting Started

 

Next we are going to wire the motor to our device

On the motor driver we are going to find the input pins denoted by driver pins labeled IN1,IN2,IN3,IN4

 

image

 

Next we will wire these GPIO pins 31,32,33,34 on the Azure Sphere. We will also connect the ground of our power source to the ground pin. Just like with the Arduino, I would not try to drive motors directly from the power provided from the Azure Sphere. With this small motor it might not be too bad, but it could definitely cause issues with motors that could draw more current.

image

IN1 = GPIO 32

IN2 = GPIO 33

IN3 = GPIO 31

IN4 = GPIO 34

Take note that the Azure Sphere's pins aren't in exactly sequential order.

 

Finally we will connect the driver to the power source by plugging the image pin to the positive of our power source and the ground pin image to the ground of our power source

image

 

My final setup was pretty much this

image

 

The Code

 

Grab the code from my Github here Github

the solution is in the "StepperMotorTutorial" folder.

 

A lot of the basic setup code comes from the Azure Sphere samples repo here. I am not a C developer(or an engineer) by trade and am still getting use to the differences of the language vs languages that I use prominently professionally day to day. The code will be "rough" and very basic, but should be very easy to follow. The code also uses the green LED and "A" user button. the button is used as the switch that starts the motor stepping process. The LED will light up to let us know when the motor should be turning.

 

Sections to take note of:

app_manifest.json

{
  "SchemaVersion": 1,
  "Name": "AzureMotorTest",
  "ComponentId": "9f089a19-d540-4b61-b413-693080f67687",
  "EntryPoint": "/bin/app",
  "CmdArgs": [],
  "Capabilities": {
    "AllowedConnections": [],
    "Gpio": [ 9, 12, 31, 32, 33, 34 ],
    "Uart": [],
    "WifiConfig": false
  },
  "ApplicationType": "Default"
}

 

This file notifies the system of the pins we intend to use. We must put in our GPIO pins for the inputs to the motor. If you use different pins remember to make you change here and to the code accordingly. We use the pins 31-34 for the motor inputs. PIN 9 for the on board green LED and PIN 12 for the 'A' user button.

 

The other section of import is the stepper motor's loop. This is the meat of what we are doing. This is timed to fire off in the smallest subset of time that the motor should move it's next step when using full steps. The code is looking for a bool that should be set when the button is pressed and then determines what is the next step it should go to.

static void StepperMotorEventHandler(EventData *eventData)
{
if (ConsumeTimerFdEvent(stepperMotorTimerFd) != 0)
{
terminationRequired = true;
return;
}


if (isMotorTurning)
{
switch (stepNumber)
{
case 0:
Log_Debug("STEP 0\n");
GPIO_SetValue(IN1, GPIO_Value_Low);
GPIO_SetValue(IN2, GPIO_Value_High);
GPIO_SetValue(IN3, GPIO_Value_High);
GPIO_SetValue(IN4, GPIO_Value_High);
break;
case 1:
Log_Debug("STEP 1\n");
GPIO_SetValue(IN1, GPIO_Value_High);
GPIO_SetValue(IN2, GPIO_Value_Low);
GPIO_SetValue(IN3, GPIO_Value_High);
GPIO_SetValue(IN4, GPIO_Value_High);
break;
case 2:
Log_Debug("STEP 2\n");
GPIO_SetValue(IN1, GPIO_Value_High);
GPIO_SetValue(IN2, GPIO_Value_High);
GPIO_SetValue(IN3, GPIO_Value_Low);
GPIO_SetValue(IN4, GPIO_Value_High);
break;
case 3:
Log_Debug("STEP 3\n");
GPIO_SetValue(IN1, GPIO_Value_High);
GPIO_SetValue(IN2, GPIO_Value_High);
GPIO_SetValue(IN3, GPIO_Value_High);
GPIO_SetValue(IN4, GPIO_Value_Low);
break;


default:
break;
}
stepNumber++;
if (stepNumber == 4)
stepNumber = 0;
}
else
{
GPIO_SetValue(IN1, GPIO_Value_High);
GPIO_SetValue(IN2, GPIO_Value_High);
GPIO_SetValue(IN3, GPIO_Value_High);
GPIO_SetValue(IN4, GPIO_Value_High);
}
}

 

You should be all set. The problems that you may run into are wiring. Just make sure that driver's wires are set to the correct pins.

 

I'd love to hear you feedback.

  • Sign in to reply

Top Comments

  • Workshopshed
    Workshopshed over 6 years ago +3
    Hey Frank, I did some work with a slightly different Azure Sphere board at the end of last year. Here's what I did for the stepper motor. https://github.com/Workshopshed/IOTCompass/tree/master/Mt3620App…
  • sleepycrat
    sleepycrat over 6 years ago in reply to Workshopshed +3
    Thanks Andy. This is really great. You included a method of step speed and rotational direction control. I am still getting used to writing code in C, so it is good to see how you created different modules…
  • Workshopshed
    Workshopshed over 6 years ago in reply to sleepycrat +3
    So my version does not have a speed control other than the wait value. But you could combine techniques from both our versions of the code and in StepperMotorEventHandler, instead of the big if statement…
  • Workshopshed
    Workshopshed over 6 years ago in reply to sleepycrat

    So my version does not have a speed control other than the wait value. But you could combine techniques from both our versions of the code and in StepperMotorEventHandler, instead of the big if statement, call STEPPER_Run.

     

    Here's a bit of an explaination about splitting a project into multiple files

    https://www.gribblelab.org/CBootCamp/12_Compiling_linking_Makefile_header_files.html

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • sleepycrat
    sleepycrat over 6 years ago in reply to Workshopshed

    Thanks Andy. This is really great. You included a method of step speed and rotational direction control. I am still getting used to writing code in C, so it is good to see how you created different modules to separate functionality. Good stuff. If I stick with the stepper motor as part of my project I will probably modify the code to use this.

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Workshopshed
    Workshopshed over 6 years ago

    Hey Frank, I did some work with a slightly different Azure Sphere board at the end of last year. Here's what I did for the stepper motor.

     

    https://github.com/Workshopshed/IOTCompass/tree/master/Mt3620App/Mt3620App

    • Cancel
    • Vote Up +3 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • noorpuri
    noorpuri over 6 years ago

    A good hint to start to work on Azure Kit

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • danzima
    danzima over 6 years ago in reply to jags_mcp

    Hi Jagdish, please see the link Frank has shared, which is also here:

     

    Free Azure Sphere Kit FAQ

     

    We expect to launch india availability for free kits very shortly, stock is being shipped to our warehouse there.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube