In this video of the workshop we will be looking at output devices, anything that allows a microcontroller to interact with the environment around it e.g. motors, lights, and speakers. We will be taking a look at how microcontrollers interact with output devices and wire and code some up to work with our home assistant robot we are constructing.

Transcript

In this chapter of the Zero to Maker workshop, we're looking at output devices, such as motors, lights, and displays that will allow our projects to interact with the world around them. If you're new to this workshop, Liam and myself will be taking you on a fast-paced and practical journey to learn a wide variety of maker skills so that you have the tools and knowledge to make anything. Follow along as we develop our projects and share insights into the process.

So far, we've looked at microcontrollers and inputs for microcontrollers, and today we're bringing this trilogy to a close by looking at outputs and getting some wired up in our project. After a fair bit of time and effort in printing, the chassis of our robot is getting very close to being finished. The last parts of it are actually printing right now. And it's at this stage now where we can start giving it some life with a microcontroller and some input and output devices.

A microcontroller can read data through inputs and sensors, and it can process that data. But what's the point of doing all that if it can't do anything with it? What's the point of doing all that if it can't interact and change and manipulate the world around it based on that information? And that's why outputs are important. An output device is a broad term for anything that we can use to manipulate or interact with the world around us, such as lights, motors, speakers, displays, etc.

We've really been trying to drill into your head the idea that microcontrollers just deal in voltages. Sensors turn a real-world measurement like temperature into a voltage that our microcontroller can read, and outputs work exactly the same, but in reverse. A microcontroller is just going to set a voltage to control these devices. And again, like inputs, we have three ways we can do this: digital signals, analog signals, and communication protocols.

When a microcontroller produces a digital signal, it's like a simple switch. It can turn it either on or off, no in-between. An example of this in action may be where we're reading a temperature sensor, and when it reaches a certain point, we could use a digital output to turn on an LED as a warning signal. Just on or off. An analog output is similar, but instead of fully on or off, it can turn partially on. You could turn it on halfway, you could turn it on 30%, 99.2% on. You can basically set any voltage between what you consider fully on and fully off.

An extremely common application of this would be to control the speed of a motor, which we will be doing later in this video. There is a caveat on this getting really technical, but microcontrollers can't produce a true analog signal, but they can instead create a really good imitation of one with something called PWM. And the last way, again, is a communication protocol. On modern microcontrollers, you'll most likely find SPI, I2C, and UART, maybe CAN bus nowadays. We aren't going to go in-depth on how to use these because we have a comprehensive beginners course that covers all these and much more. Link below.

Alright, we need some outputs for our robot, so let's take a look through some devices and kind of learn what we have choice-wise and find some for our project. So first off, we want our robot to be able to move around, so we're going to need an actuator. An actuator is just anything that turns electrical energy into movement, such as motors, servos, steppers, solenoids. You can add that to your fancy learned vocabulary. The first thing we need to do is decide on what actuator is most suitable for this project. And I say most suitable because there's often more than one actuator that you could use for a given application.

Actuators tend to provide either rotational or linear motion, linear being motion in a straight line, so things like solenoids and linear actuators. But we want to rotate some wheels, so that narrows it down to rotational. And three that spring to mind are servos, steppers, and DC motors. Servos are great, but not a good pick for this application. They are super handy in that we can set an exact angle and they will move to that every time. But they are limited in rotation, usually they can only move 180-degrees. And we need the wheels to spin continuously over and over. They can be modified to spin continuously like that, but that is a whole other can of worms.

Stepper motors, this is a really tiny one here, are a viable option for this project. They're kind of halfway between servos and DC motors, they can spin continuously. And although we can't set them to an exact angle like a servo, we can accurately rotate them by a certain amount. So we could say, wherever it is now, rotate by 90-degrees, and it will precisely move that much. But for this project, we're choosing the humble DC motor. We can't set an angle like a servo or stepper, the only thing we can control is speed. And I chose it over a stepper motor because it's going to be faster and much simpler to use.

Okay, so we've chosen what type of actuator we're using. There's a few more things that we need to decide upon though. The first being, how powerful of an actuator do we need? Too small of an actuator, in our case, something like this tiny DC motor, and it's not going to be able to apply enough force to move our robot. It's a better idea to get an actuator that's slightly on the larger side than what you need. So you know it has enough oomph for your project, but not too large as it may consume a lot of power and might be a fair bit more costly. And that's kind of the trade-off between cost and performance, you know?

I've done a bit of math and I figured out that this motor here is the right size and power for my needs. Maybe a little bit overkill, but that's going to make our robot really snappy and be able to move really rapidly. Selecting an actuator for your project is a bit of an art, but we have an entire guide on it where we go in-depth on all of it. So check it out in the link below. Something else we'll need for our motor is something called a motor driver or controller. And we'll be talking about this in just a bit. In terms of other outputs, there's a lot that I want this thing to have, like speakers to be able to communicate with sound, maybe a touch screen that's not only an input but an output as well so we can display information. I want to put lights on it to, well, make it look cool.I'm holding off all of these things for now because the project isn't quite ready for them yet. Something I do want though is some sort of visual indicator to see what's going on. And for this, I'm going to use one of these OLED displays. This is going to help us with developing code for the robot. For example, let's say I was trying to program this thing to look for a piece of rubbish on the ground and then pick it up. On this screen, I could print out what the robot is thinking or what steps it's currently taking so we can see where it might be failing if it's having problems. These OLED displays are super cool and easy to use. They're just a black and white screen that you can use to display information with. I have a huge soft spot for these because they're so cheap and they can just be whacked onto any project.

Now that we have our outputs, let's wire them up. And connecting up our outputs to the microcontroller is actually the hardest part of using outputs. The problem is, is that the microcontroller can't supply much power. For example, this motor needs thousands of times more power than this Pico can provide. This motor has lots of wires coming out of it and most of these are just for the rotation sensor that's on the back. But the only thing we need to control it are these red and black wires here. We provide a voltage across these and it will spin the motor. The Pico cannot provide enough power to do this though. We could connect it to another power source that can provide enough power like a battery or this benchtop power supply, but when it's connected like this, it's not plugged into our microcontroller so we can't use code to control it.

This is where a motor driver comes in. A motor driver is just a piece of hardware that lets you power the motor with an external source like a battery, but it takes the output control signal of a microcontroller and uses that to control the motor. In our case with the Pico, we can use PWM to create an analog output with a voltage between 0 and 3.3 volts. If we set that voltage to half of 3.3 volts, the motor driver will see that we've set that signal to half strength and will supply the motor with half the power from the external power supply. And nearly every actuator will need to use a driver board like this if you want to control it with a microcontroller. Except for servos though, because they kind of have one inbuilt into them and they're just a lot easier to use because of that.

You may also find outputs on a module, and a module is just a self-contained board that makes it easier to power and control things. This OLED display is on a module and all we need to do is plug in power through these two pins and connect these two signal pins to the Pico. Super easy. This board uses I2C and a lot of module boards will use a COM protocol like this to communicate with the Pico. There is one more thing we need to talk about when wiring up outputs, and that is voltage and current. Water in a pipe is the common analogy for these, with voltage being like water pressure and current being the flow rate of water in the pipe. And when it comes to powering your projects, the rule of thumb is to ensure that the voltages match and you can supply enough or more current than needed. For example, this OLED screen runs on 3.3 volts, so we need to ensure that it is powered with 3.3 volts. If we plugged it into 2 volts or 5 volts, we may damage it or run into some issues. It also draws 30 milliamps of current, 1000 milliamps is 1 amp, like kilometers and meters, and the power source we're using for it can supply up to 300 milliamps of current, which is more than it needs, and it'll be fine as long as it's more than 30 milliamps.

Power in the context of maker projects is really simple, but possibly one of the most important things to learn, and because of that, we have an entire video in the Pico workshop, which we have a link to below. Now that we've selected our parts, the last thing we need to do is wire and code it all up. Now just to demonstrate it, I've gone ahead and taken one of the wheels off of the robot and connected it up to the motor driver and the Pico. This isn't going to be a how to connect up a motor driver guide because everyone is slightly different, but long story short, you're going to need to connect three pins per motor. Two of them are going to be digital outputs that you either set high and low or on and off to control the direction of the motor, and the third one is going to be a PWM, which is our analog-like signal. And we're going to use that to set the speed, so we can control direction and the speed as kind of a percentage.

So here's our motor connected to our motor driver, which is going to supply power through these two wires, and the motor driver takes a signal from the Pico to control the motor, but it takes power from this external power supply here. And I've gone ahead and written this really simple code here. It literally just turns the motor on for a few seconds and turns it off. Just a simple test to ensure that all of our outputs are working. We start by importing the libraries and the modules we need. Then we set up the PWM pin, which we're going to use to control speed, and then the two digital output pins that we're going to use to control the direction of the motor. Then here we create some variables for the speed of the motor as a percentage and the direction we want the motor to go in. And then we basically say, if the direction is forward, then we set one high and the other one low, and that makes it go forward. But if it's reverse, we swap it around, which changes the direction. Then here we actually set the PWM to the motor. We let that run for five seconds with a sleep, and then we turn it all off. Just a really simple test. And if we turn on the power supply and run that, we can see that our motor spins. And the way I've written this code makes it really easy to change everything. So let's set it to 100% speed and make it go forwards instead. And run that. And that is a bit better. And now with those three pins, we can control the motor however we really like. We can go ahead and code up the rest of the project and use it in whatever way we plan to. I've also gone ahead and plugged in the OLED screen. All I need to do is supply it with power and then connect two pins to the Pico, and it's going to work via I2C. Here's the same code as before, but all I've done is modified it to display what we're doing on the OLED screen. And all I've done is imported the library here, set up the screen as an I2C device here, and if we come down, I just print speed. This is the speed. And then I say direction, and I print out the direction. And if we run that code, you can see that our display is now printing out some information. And that was all of our outputs working at a very basic level, which is probably a bit of a smart idea to do.

It's called working in loops, where you start on something simple, and then you come back and, oh, it works. So then you get a little bit more elaborate and a little bit more elaborate. So what I think I'm going to do next is I'm going to connect up all the motors and get them all driving around, and then I'm going to implement some math and make it move around and dance around and really use those mecanum wheels. And with that, we are done. And this thing is monstrously huge. It's about 80 centimetres wide when it's fully stretched out. I'm just going to see if I can lay it down here. It barely fits on the table.

And all I've done here is just repeated the exact same thing, motor to motor driver, plug it into the Pico, and then code it up to control each motor. It's driving around pretty nicely. And my math is a little bit off and it's driving a little bit crooked sometime, but that's just kind of a fixing the software and my logic in my code. And with that, most of this project is actually finished. The only thing we might need to add is some suspension and maybe some servos to actuate these arms up and down. But we're very close to our robot being able to drive around, and the only thing from there is adding more and more code to it.

And that does bring up something. We've got our inputs, our outputs, and microcontroller. And once we've got it all on there, the only kind of thing that we can improve from there is the software on it, which is why learning to code is really important. Just being able to give it more and more advanced thinking to do more elaborate tasks. I know we've said it a billion times, but go out and watch the Pico workshop. We teach you how to code for the Pico and how to wire stuff up to it for your project. We really just tried to condense all the essential knowledge into one course. You can find links to that and everything we mentioned in the course page below. Till next time.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.