In this video, we will be looking at input devices (or sensors) and how we can incorporate them into our microcontroller-based projects. We will be taking a look at the role of input devices, and how they work in general terms, and we will be looking at some guidelines on choosing a sensor for your project, as well as wiring some up.

Transcript

In this chapter of the Zero2Maker workshop, we're diving into input devices. The first step in automating a system. We'll be looking at the role of sensors, some tips on choosing one, and wiring them up and collecting the data. If you're new to this workshop, Jaryd 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 own projects and share insights into the process.

Input devices let us gather information about something happening in the real world. These may be as simple as the humble button, or a temperature or motion sensor, basically anything that's measurable. Even a camera counts. Using the analogue of a human, if the microcontroller is our brain, which Jaryd discussed in the last chapter, inputs are our sensory organs, like our eyes, ears, nose, and anything you can sense something with. But why are inputs important for our projects? They're the very start of the automation process. We need to detect what is happening in the real world so our project can take that data, process it, and respond somehow. For example, a car's cruise control might use the wheel speed sensors to maintain the speed, or a production line will have tons of inputs in the form of safety switches, product weight sensors, and maybe even a console with some buttons and dials so humans can interact with the system. And even time is an input, one of the most essential and overlooked inputs, used by something as simple as your phone's alarm. Automation is all around us, and it is driven by inputs.

These inputs translate real-world information into data in the form of an electrical signal. Microcontrollers can read these signals and get real-world data equivalents inside of them. These signals come in many, many forms, and there are three main ways that our microcontroller can read them. The easiest is a digital signal. Like a light switch, the power or voltage can either be on or off. There isn't an in-between. We could plug a button into our microcontroller and it would behave much like our light switch. A bit harder, an analog signal is like a dimmable light. We can pick a value between fully on and fully off. This input can set the voltage based on what we are looking to sense. For example, we could plug a temperature sensor into a microcontroller, and as the temperature rises, the microcontroller will see a rising voltage, and vice versa. And the most complex is using a communication protocol. We can quickly change a digital signal to add more information than just on or off. This is like Morse code, where we use a single button to send whole sentences, and loads more information. You might have heard of some common communications like I2C, SPI, Serial, CAN bus, USB, and there's so many more. We won't dive into them here, but we've talked about some super common ones in-depth in our Pico workshop. Links will be in the resources section below.

We know what an input is, and how a microcontroller can get data from one. But how do we know which is best for our project? There are probably lots of options, but which one should we use? Let's take a look through all of the key aspects of selecting a sensor while we work on my project. Plant Pulse. It's an agricultural system that can detect the health and take care of a whole plot of plants. First up, are sensors available to directly measure what we need? Temperature and humidity affect plants, so we want to measure it, and there are sensors to directly do that. I've selected the PiicoDev Atmospheric Sensor that has a suitable measurement range and really good accuracy, and it includes all of that in one neat package. So let's add this to the cart. Another thing I want my project to do is measure the general health of plants and how they're feeling. We could use a colour sensor for each leaf, but that would be very expensive and annoying. We would need so many colour sensors to manage a whole plot of plants. But a camera is a sensor, and that takes a photo that we can quickly check, or maybe even AI could, and we can get a very good judgement of how the plants are doing from that quick photo. So let's use a camera. Outside of my project, but a great example, how could we measure the amount of water in a tank? It could be hard to find a direct sensor to measure the tank's level, so I'll have to think a little bit outside the box. We could use a distance sensor to read the distance to the top of the water level, or even a weight sensor underneath to weigh the amount of water. Hopefully that has you thinking about how you can directly measure something versus something a little bit more creative or indirectly.

And two, price versus performance. There'll always be a trade-off between the two and sensors for your projects. We need to make a good value judgement on the performance of our sensors. We need to make sure our plants are watered and they won't wither away. So let's measure how much water is in the soil. We could use a $1,000 industrial-level sensor, but do we really need that? This $3 sensor that needs calibrating on a plant-by-plant basis is perfect for our homemade solution, so let's use that one. Another example is measuring the temperature. We could use a super expensive temperature sensor that measures to a thousandth of a degree, but the selected PiicoDev atmospheric sensor measures within half a degree, which is great. And we're using that over the expensive one since we just need a ballpark measurement. And that's price versus performance.

And three, documentation. How long will it take us to get these sensors working? Our PiicoDev atmospheric sensor includes excellent examples and an example code repository that explains how to use each of the functions, whereas a cheaper vendor might not include any example code or even a library. So we'd end up saving some cash purchasing a cheaper sensor, but we'll probably end up losing more in time. And there's a couple of other considerations we need to make to ensure our sensors work. Our microcontrollers will have a logic level voltage, and we need to make sure that they match the sensors. Some common ones are 3.3 volts and 5 volts. Just make sure that they match. We should also check that our sensors are available and that they're in stock and ready to ship. And speaking of, I've just placed the order for our sensors. Hey, we've got our sensors. I'll quickly wire them up and we'll be right back. And when I've been wiring these up, I've been checking the ASP32 pinouts, the MicroPython manual, and the product pages for each of the sensors I'll be using here. They're connected, but how do we get the data off of them? We need to program our microcontroller to interact with each sensor to get our measurements off of them. We'll be using MicroPython through the Thonny IDE, and we won't be programming each example from scratch, but I'll explain what we're doing as we go through the process. If you're not about coding, check out the Pico workshop, link below.

So let's try and find an example for our soil moisture sensor through Google. We know that it's an analog sensor. We're running MicroPython on an ASP32. So let's string all of those together and search on Google. And the second result here is from the official MicroPython documentation. So let's take a look at that. Here we've got a generic development board, but we're interested in this section here, analog to digital conversion. Here we've got some example code, and I'll take this, read through the rest of the documentation here, and get some code ready so that we can use our soil moisture sensor.

I've got everything we need to test our sensor. Let's walk through the code first. First we import all of our required modules. Then we set up a soil moisture object so that we can read or adapt the settings for that particular module. Then we enter our loop. So first we get the value from our analog sensor, our soil moisture sensor. Then we print that out to the shell down here, and we wait a second. And this will continue to run until we interrupt our microcontroller. Let's give it a test. Here we've got some values. While it's dry, it's sitting at about 47,000. If we dunk it in water slowly, we can see that value decrease very quickly. And as we get up to the line here, we can see it sits at around 17,000. And if we vary it a bit, we can see the value varying down here in the plotter. That's awesome. We know that our soil moisture sensor works.

And onto the PiicoDev atmospheric sensor. I've had a look through the guide on the website for the Pico, and I've just adapted that a little bit for the ASP32. So let's open up that example code, and I'll talk you through that as well. First we import all of the modules. Then we set up our atmospheric object. And this varies a little bit from the normal Pico version on the website, since we're using an ASP32. Then we enter our loop. We get all of the values off of the sensor, do a conversion, and print them out in a nice format. Then we sleep and do all of that again. So let's run the example. We've got a little warning followed by all of our data. So let's increase the temperature by quickly putting my finger on top. And the temperature's increasing. Now let's test the humidity. And we can see both of those spiking as we interact with the sensor. So now we know that the atmospheric's working.

And finally our camera. I actually wrote the driver for this guy, so I know the ins and outs, but it's still a very easy driver to use. And here we are. I've copied the example into Thonny. First of all, we import all of the modules. Then with these three lines, we set up our camera. We can set the resolution, we wait a second for the camera to warm up, we take the photo, and then we save it. So let's do exactly that. Frame it nicely. And now it's saving. So we'll just wait while this happens. Should take a couple of seconds. And that's done. Great. We need to refresh the file system in Thonny. And then we can download our image. Switch over to our downloads. Great, we've got our camera working.

And for all of these sensors, we could keep the data somewhere, which is also known as data logging. We could use the onboard memory or a microSD card, but there are many other ways to do this. But I've gone and created what's called a CSV with the soil moisture and environmental data. Or we could even transmit it to an external device or an online dashboard, but we'll cover that in a later chapter. And that's getting all of our sensors working. We'll talk about outputs, wireless communication, and integrating them all in coming chapters. So stay tuned. Thanks for watching and happy making.

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.