Simple GPIO Motor Control

Hi tecsploiters!

this post walks you through how to  get basic control of an external motor using one of the STM32F4 Discoveries GPIO pins working. This isn’t the best way to control a motor- so I wouldn’t recommend you try hooking this up to anything important! but its a good place to get started if your trying to learn the basics!

Simple Motor Control

Simple Motor Control

What you will need for this project:

  • STM32F4 Discovery board
  • Some connector wires
  • A battery / battery pack
  • A couple of resistors
  • An NPN transistor
  • An Led

The circuit diagram for this project is shown below, as you can see we are using the transistor as a switch to control the LED and the motor,  the GPIO pin is going to be toggled between 0V and 3V, when its at 3v the transistor will ‘switch’ the circuit on resulting in the motor running and the LED lighting up, when it falls down the 0V the transistor will switch the circuit off causing the LED to turn off and the motor to stop.

The code for this project is the same as the code used in my previous post on controlling and  LED with an external power source, all its doing is cycling the GPIO pin on and off. If any part of this project is going to cause you trouble it is the transistor. The key is to know what type of transistor you have and what lead is what – the easiest way to find this out is to look at the documentation. This circuit is designed to work with an NPN transistor, this type of transistor will be switched on when a positive charge is place on the base lead (ie are 3V from the GPIO pin ), this transistor should be placed on the negative side of the circuit.


Simple Motor Control

Simple Motor Control

If you use a PNP transistor by mistake you will get strange results! You can create this project using a PNP transistor however the transistor should be put on the positive side of the circuit, and you will find that the switch may be on by default, and turn off when you the charge on the base pin is 0V.

If you dont know what type of transistor you have I’d recommend you create a simple circuit with an LED to test – ill create a post on how to do this if I get enough comments asking me too! And dont forget to subscribe to my email list to be notified of any new posts!

See the video of the motor in action on youtube


Leave a Reply

Your email address will not be published. Required fields are marked *