In this video, we will be taking a look at microcontrollers, the brains of a maker project that controls and orchestrates it. Microcontrollers are the bridge between the components of our projects like motors, lights, and sensors, and the code that we can write to control all of them. We will not only be taking a basic look at how microcontrollers work, but also be doing a practical demo and explaining some of the magic behind that demo.

Transcript

In this chapter of the Zero to Maker Workshop, we're diving into microcontrollers, the brains of our projects that allow them to sense and react to the world around it. 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. So far we've looked at how to model and manufacture our project, and we've started to build the chassis of our home assistant robot with 3D printed parts, and it's starting to come together nicely. But the next thing we need to explore centres around these microcontrollers. This robot is going to have motors, LEDs, distance sensors, a whole heap of electrical devices on it, and a microcontroller board is going to be the computer that will coordinate and control all of them. A microcontroller is just a tiny little computer, but it doesn't run Windows or have a mouse and keyboard, nor does it typically plug into a computer monitor. Instead, it's a tiny computer that has these pins here, and we can plug our motors, sensors, and all of our electrical device into these pins. Then we can write some code, upload it to the board, and this code that we put onto it tells the microcontroller what to do with the things we've plugged in. So in our case, this robot's probably going to have a whole bunch of sensors detecting the world around it, and the microcontroller would read these sensors, and we might write some code telling it that if it's driving and a wall is less than 10 centimetres away, stop the motors so it doesn't crash. Or maybe if I press a button, drive forward two meters. Whatever we want the robot to do, we will need to program the microcontroller and tell it how to do it with what we've plugged into it. And you'll find these things in everything. You'll find them in washing machines, your computer mouse, cameras, microwaves, drones, electric toothbrushes, and they all play the same role. They all control and orchestrate all the sensors and motors and whatever electrical devices these have in them.

Now there are a lot of different microcontroller boards we could use for this project. You might have heard of ones such as Arduinos, ESP32s, Teensys, but we will be using a Raspberry Pi Pico. Why? Well, we could have an entire video looking at the pros and cons of all these different microcontroller boards, but long story short, the Pico is pretty fast. It has a decent amount of pins that we can use. It's got Bluetooth and Wi-Fi capabilities, and it's a bit less expensive than the other options. So realistically, I could have used any of these microcontrollers, but the Pico is a better fit. And there really isn't a wrong or right choice in microcontroller. It's more so choosing one that best suits your project. For example, Liam is using an ESP32 because they have some features that he really needs to use, and he's more familiar with them. Another consideration to make is what programming language will we use? We don't use human language to tell a microcontroller what to do. Instead, we need to use a coding language that the microcontroller can speak. And the two titans of this that you will encounter are C++ and MicroPython. They both have their strengths and weaknesses, but long story short, MicroPython is a bit easier to use and more beginner-friendly, but C++ can be more powerful in what it can do. However, it's not often that you need that extra oomph that C++ can provide with microcontrollers. But don't feel like you're locking yourself in by learning one of them. Knowing the logic and thought processes behind coding is the important thing. So once you learn one of them, it's pretty easy to learn another language. I learned C++ first, and it only took a few days to learn MicroPython. And for this project, we'll be using MicroPython because it's just a lot easier to work with.

Now we are going to jump into a demo of MicroPython on the Pico to kind of demonstrate how this all works, but we aren't going to go in-depth into it. The reason is that we have an entire beginner course on the Pico and MicroPython. It's a very practical course that teaches you all the essential skills you'll need as a maker in bite-sized videos, and you'll learn everything we'll do here in a lot more depth. So if you don't already know how to code and use microcontrollers, it's a great opportunity. With that in mind, let's get started. First things first, let's start by plugging in some simple hardware into our Pico. I'm just going to plug in a button like so, and then plug in a LED like so as well. And one more wire to connect it to. Now, microcontrollers will have a certain selection of pins that you can use to plug things into, and they are all numbered. You'll be able to find diagrams for these quite easily. And in this case, the button is plugged into pin one and our LED is plugged into pin 16. These will be important to remember for later. And here I am in a program called Thonny. This is software that we'll be using to program our Pico with. Now, if your microcontroller is brand new and hasn't been used before, you'll need to set it up and install some firmware onto it before you start programming it. But this is usually a really easy process in a program like Thonny. This Pico has already been set up so we can start coding right away. And here is a very basic program I've written for this hardware. Now, code is going to read from top to bottom in order, and most code is going to sort of follow the same structure. At the very top here, we start by importing the required things that we need to build our program with. These are called libraries. After that, we go through and set up the other things we need. So here we're setting up the hardware that we have plugged into our Pico. We're saying that we've got an LED plugged into pin 16, and we've also got a button plugged into pin 1. Remember how we said those numbers would be important for later? These will be important to remember for later. We need to tell the Pico where we've plugged this hardware, and then this is basically telling the Pico the things that are plugged into it and in which pins. Then after that, we have our while true loop, and this is just going to repeat the code inside of it over and over until the end of time or until we turn off the Pico. And in here is the meat of the code.All we're doing is checking the state of the button here, whether it's pressed or not. And then we say, if the button is pressed because one is pressed, turn on the LED with one being on. And if it's not pressed, turn the LED off. And this code will be run over and over as fast as the Pico can run it, probably in the range of tens of thousands of times a second. And this is running so fast that it basically looks like it reacts instantly.

If we plug in our Pico and we run our code like so, we should be able to now press the button and the Pico detects that we're pressing the button and then turns the LED on, just like we have programmed it to. But you could do this very easily without a microcontroller. A button is just a switch. And if we connected this LED to a battery with the button, we can just press the button and the LED will turn on.

Well, this is where microcontrollers get really powerful. With the exact same hardware plugged in, we can wildly change the behaviour of the system by changing the code. For example, I can come in here and I can add a sleep function, which is basically just going to pause the Pico and I'm going to tell it to sleep for three seconds on that end and sleep for three seconds on that end. And if I upload this code to the Pico, now when I press the button, it should wait for three seconds as it goes into that sleep and then turn the LED on. Completely different behavior.

Let's modify the code again. And I'm just going to copy and paste this in because it took a while to write out. But all I'm doing is I'm turning the LED on, putting it to sleep for a little bit, then turning the LED off and chaining a whole bunch of these sleeps and LEDs on and offs so that if now I run it and I press the button, the LED flashes out an SOS in Morse code. Again, completely different behavior, exact same hardware.

Or how about we get a little bit fancier and instead of turning it on or off, we slowly increase the brightness of the LED until it's fully on like so. We can turn it off and let's just fully turn it on again. There are hundreds of examples that we could do here, but the important thing is that we can change the behaviour of the hardware plugged into it just by changing the code.

This button and LED example might seem a little abstract or a bit of a basic demonstration, but we're kind of halfway to the moon here. It's not too much more work to control a motor in a robot or the headlights of your car or a water pump in a washing machine. And that's because all microcontrollers are doing is reading and controlling voltages.

We have this button wired up so that when we press it, we supply a voltage to the Pico. The Pico doesn't know that it's a button that we've plugged in or how it works. It's just reading that change in voltage as we press the button. And the sensors that you can buy and plug into the Pico are just setting voltages. Liam's project has a soil moisture sensor, and all it's doing is measuring the moisture level and sending a voltage back to the Pico for it to read. And when the Pico turns on the LED, all it's doing is setting a voltage on that pin. Again, it doesn't know that it's an LED there. It's just setting the voltage on. And we can use this to control motors and fans and lights and whatnot.

In this last example, when we were slowly increasing the brightness of the LED, all we're doing is we're slowly increasing the voltage we're supplying to the LED. And in the same way we do this, we can slowly increase the speed of the motor or we could decrease it or we can set it to certain speeds or whatever we need. The main thing here is that nearly all the electrical devices around us simply deal in voltages, and we can use a microcontroller like our Pico to read and control these. It orchestrates all of these and you can tell it exactly what to do and how to do that with code.

So LEDs and buttons are not too far off controlling a giant robot. In the coming videos, Liam and I are going to start adding some sensors and electrical devices to our projects and start controlling them with some code and microcontrollers. So stick around if you want to see a bit more of a real world how to plug things in and what sensors and devices you can use. And again, if you want to learn microcontrollers and coding in greater depth, we have an entire course for it. It's free and it's designed to take you from an absolute beginner level to having enough knowledge to go out and start implementing microcontrollers and code in your own projects. 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.