In this video of our Meshtastic for Makers course, we're going to look at how to use the detection sensor module to send the on or off state of a simple digital sensor without needing a second Pico, as we've been using previously. We'll set all of this up on the Pico, which we configured in a previous video. You can find the entire course playlist and the written course linked below.
Before we begin, it's important to clarify that with this method, we're only able to send a digital state—either an off or on signal, like a button. This could be a button or a switch monitoring a gate or a door, a motion-sensing PIR sensor (which we will set up in this video), an infrared sensor, a reed switch, a water level sensor, and more. There are many sensors with a digital output that we can use in this video. The trade-off here is that you can only use a simpler sensor, but there are fewer moving parts. You don't need a second Pico, and you probably don't even need to code anything.
Let's jump into it with a simple button example. We are going to slot our Pico into the breadboard with the header extenders we've been using so far. Then, we'll simply connect a button from ground to pin 21, which we found to be a compatible pin with this method. Next, we'll open our Meshtastic client and head over to the detection sensor module.
Starting off, we're going to enable it. We have two settings here, which are different ways of sending that data. The first setting allows you to send a message when the button is pressed. Only when you press the button will it send a message to the network. This time here is how often that message can be sent. For example, if we set it to three seconds, I press the button, it sends the message, and no matter how many times I press that button again, it won't let me send a new message until three seconds have passed. After three seconds, I can press it and send another message, starting another three-second timer. This is great because, let's say it's connected to a door; as we close the door, it might bounce and tap the button a few times. We don't want to send that message multiple times, so by putting a few seconds here, we can stop that from happening.
I'm going to set it to zero to disable it, and then I'll put in five seconds for the other one. This one is simple: every five seconds, it will send the state of the button. If it's being pressed, it'll report one; if it's not being pressed, it'll report zero. You can change how often you want this to be sent. This might be a better option if you want to know if a gate is open or closed, as you could check the switch periodically. For the rest of this video, we will set this to zero to disable it and use the first one, but it's worth knowing that it's another option.
We're also able to set a friendly name here. The message being sent will start with what we put here, followed by something else. If you're using the same messaging system we've been using before, you can put something like "front gate:" or "door:" or whatever you need. We'll look at what this output looks like in a bit. Then, we'll set this up on pin 21, which is the one we're using, and ensure that the top one is off and the bottom one is on.
The way we have this set up is that when the button is pressed, it will bring the voltage of the pin down to zero volts or low. Turning off this first setting triggers the pin to do something when it changes to zero volts or low. The second one enables a pull-up resistor inside the Pico, ensuring that when we let go of the button, it goes back up to 3.3 volts or high. So when we press it, it goes down, and when we release it, it springs back up to 3.3 volts.
Let's save that and give it a go. My phone is connected to another Meshtastic device, and we're just looking at the messages coming from it. If I press the button, it should come up with "Door Detected." As you can see, I can press the button a few times, and only one message will be sent because of that three-second cooldown. If I press it now, a new message is sent, like so. Unfortunately, it does have a space after the colon, and we have been working without spaces so far because it keeps it simpler. So if you are going to use one of these and write code on another node to do something with the message being sent from this one, just be aware that it has the space in it, and you will need to deal with it.
For the sake of completeness, I've got the other mode here, which sends the message every five seconds. As you can see, it's sending through state zero because the button's not being pushed. If I press it down, it will send state one, and that's just going to be sending the state of the button every five seconds.
That example used something called an active low setup, which means that when we press the button, it connects the pin to ground, bringing it down and activating it. Let's now take a look at how to do the opposite, where we trigger the message when the pin goes to 3.3 volts or active high. Why would we want to do this? With a button, we have the luxury of wiring it up however we want, but with something like this PIR sensor, it outputs zero volts when there's no motion being detected. When it detects something moving, it sets it to 3.3 volts, so we need to use this active high setup.
Let's wire it up. We're going to get rid of that button and sit our PIR sensor on the side. Then, we need to power it by plugging it into the power output pins of the Pico. We'll ensure that we plug the signal pin into pin 21 of the Pico. Super simple—let's plug it in and get to work.
Back in our module settings, we'll want to turn on the first one because we want to trigger when the pin goes high. Then, we'll disable this pull-up sensor. We'll also use this opportunity to change the message to "PIR," which will change the message being sent.
Most sensors like this do not need a pull-up or pull-down resistor to work. However, if you are using a button or a switch and want to use it with this active high mode, you'll probably need to manually wire one up. Unfortunately, Meshtastic, at the time of making this video, only supports pull-down resistors with software. So if you want to use this method, you'll need to make your own. If you want to use the button, it's probably easier to use the active low method we looked at. This active high method is more for sensors.
I've currently got that running, and I'm staying very still so that the motion detector doesn't detect me. If I move, it should set it to high and send that message. It's only going to send every three seconds or however long you set it. I'm probably moving a bit too much. When I move, it sends a message. When I stop, and then move again, it sends a message.
That's how you can use simple digital sensors to send messages over the Meshtastic network. We are limited to simple on/off messages, but as you can see, the setup is much simpler. If you get creative, there are many things you can do and measure with a simple digital sensor. If you made something cool with this or need a hand with anything we covered in this video, feel free to head over to our community forums. We're all makers over there and happy to help. Until next time, happy making. Thank you.
Makers love reviews as much as you do, please follow this link to review the products you have purchased.