Robotics & Engineering Teacher Training
20 mins
+90 XP
What you need:
Chromebook/Laptop/PC

How to program a Motor

In this lesson, you will learn the basic concepts and techniques of programming a motor for desired movements and actions.
Learning Goals Learning Outcomes Teacher Notes

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Understanding Motor Control

    In order to program a motor, you'll need to understand the basics of motor control. Motors are typically controlled by sending electrical signals to adjust their speed and direction. These signals can be generated by a microcontroller or a motor driver circuit.

    2 - PWM and Motor Speed

    Pulse Width Modulation (PWM) is a common technique used to control the speed of a motor. PWM is a method of generating an analog signal using a digital source, such as a microcontroller. By adjusting the duty cycle (the percentage of time the signal is high), you can control the speed of the motor. A higher duty cycle results in a faster motor speed, while a lower duty cycle results in a slower motor speed.

    3 - Motor Direction

    To control the direction of a motor, you can use an H-bridge circuit. An H-bridge is an electronic circuit that allows you to control the direction of a motor by applying voltage to its terminals in different configurations. By connecting the motor terminals to the H-bridge outputs and controlling the input signals, you can make the motor rotate in either direction.

    When using a micro:bit with a compatible motor, you have the advantage of automatic motor direction control. This means that you don't need to manually set up an H-bridge circuit, as the micro:bit and motor driver will handle the direction control for you. To change the motor direction, you simply need to send the appropriate commands to the micro:bit, which will then control the motor driver accordingly.

    4 - Microcontroller and Motor Driver

    When programming a motor, you'll typically use a microcontroller (such as an Arduino or a micro:bit) to generate the PWM signal and an H-bridge motor driver to control the motor direction. The microcontroller sends the PWM signal to the motor driver, which then adjusts the voltage applied to the motor to control its speed and direction.

    5 - Video

    The video demonstrates how to program a move motor car that incorporates a micro bit. It begins by introducing a car with a micro bit slotted into it, aiming to give instructions to the car's two DC motors. Using a project editor, the presenter adds an extension for the move motor, allowing them to program and interact with the motor's functionalities, such as direction and speed. 

    Duration: 6:11

    Video transcript: we're going to be programming this move motor car as we can see it's got a microwave slotted into it already so we'll be programming the micro best to give instructions to the two DC motors that are on this car I've created a new micro bit projects down here in the project editor and the first thing we're going to do is just add in an extension for this move motor so motor search selected here and what this will do is we'll add in a category of blocks where I can program the new motor so I can interact with the light sensors motor sounds pins what we're going to be doing today is the motors so first of all let's just program one of the motors to spin forward I'm going to need an input so I'm going to drag in an on button a pressed zoom in a little bit and we will go into the motors and get a turn let's turn the left motor on Direction forward at speed 10. so as we can see we can choose which motor we want to program and we can also choose which direction so we can make the motor go forwards or reverse so we can also choose the speed the speed input we can go from zero all the way up to a hundred so we can give what value of speed we want to set the motor so let's just go where 10 and I'm going to quickly download this onto my micro bit so here's my move motor car with the program downloaded onto the micro bit so first I'm going to turn it on and now when I press the a button it should spin the left motor which will spin the left wheel forwards at speed 10. so let's try it out press a button and as you can see there the left motor is spinning the wheel the next thing we're going to do is we're going to program the same motor the left motor to move in Direction reverse so let's program the B button to do that so we're going to sew so on B button press turn left motor on Direction but this time reverse and we'll go at the same speed at 10. so again I'm going to download onto my micro bit and now let's test the other code for the B button work so again the B button should be spinning the left motor in Reverse this time so let's turn it on press the B button and we see that the wheel goes in reverse so if you press a go forwards B reverse so now that we've programmed the motor to go in both directions forward and reverse let's program the speed this time so to do this I'm going to create a speed variable so make a variable we'll call it speed and we will set speed on start to the zero and when we press the A Button we will increase the speed so let's say change by 10. and then finally we will put the speed variable into the turn motor block actually we'll put them put it into both of them what we'll also do is we'll program the B button when we press the B button it will change the speed by -10 so a will increase the speed and B will decrease to speed so we'll be able to play around with the speed by interacting with the buttons and the micro mode so let's download and see how this works so now each time I press the a button it should make the left wheel spin forwards but each time I press the a button it will increase the speed by 10 and so I'll press it up up and it'll make the wheel go faster when I press the B button it'll make the wheel go into reverse and also use the speed variable whatever is currently at but each time I press it it'll take 10 away from it so let's see how it works turn it on press the a button so we're moving forwards faster faster and faster press the B button so we go into reverse still using the speed variable but each time I press it it reduces by 10. so forwards and reverse so we can even stop it or we can make it spin very quickly so that is how we program a motor so we've multiple motors on this car so first of all we need to select which motor we are programming but then after that we simply need to set which direction it's going forward or reverse or clockwise and anti-clockwise and then we just need to program what speed it needs to rotate at and that's it for Motors now if we've got multiple motors say on a car such as this we can use them for going forwards or reverse or turning left or turning right for example if we wanted to program the car to go forward so let's just clean up a little bit of code here okay we don't need all these we won't use the variables for this so say we wanted to program the on button a press to make the car move forward well what we could do is we could spin both the motors together so we're going to turn the left motor on the right motor in the direction forward at speed 10. so that will move the car forwards now if we wanted to make the Carver first if you wanted to program that let's duplicate these again get a left motor and a right motor and this time we will say reverse at speed 10. if we wanted to make the car turn left what we could do is we could stop the left motor so we could you could say speed zero for the left motor and just spin the right motor so that would make the car turn to the left so that's an example of how you would program two Motors together to go either forwards reverse make the car turn left Mark make the car turn right so that's essentially how we program Motors we choose the direction and then we also set what speed it is to rotate at

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more