The latest Circuit Playground board, the Adafruit Circuit Playground Express comes equipped with an onboard IR transmitter and receiver! This can be used to communicate with TVs and other household devices. You can also use infrared remote controls to control your Circuit Playground Express. You can use another Circuit Playground Express as a remote to control your CPX, and you can even send simple messages between Circuit Playgrounds. By measuring the raw reflected IR light bouncing off of nearby objects you can make a simple proximity sensor!
Hi, Steven here from core electronics, today I'm going to talk to you about how to use the infrared transmitter and receiver on the Adafruit Circuit Playground Express and we're going to do it using MakeCode.
Utilizing these sensors is easy and MakeCode and it's an exciting feature that adds the remote option to any project that you might be making, and it allows you to use two Circuit Playground Expresses and have them communicate with each other. Every Circuit Playground Express has a transmitter, next to the A button and it's this little white LED and that's an IR blaster and then we have a receiver diode, which is this black diode next to the B button and then there's a decoder chip, which decodes all the pulses of infrared light into a digital signal, that you can recognize and use within MakeCode. So we'll take a look at the program that we're going to make it's going to use two Circuit Playground Expresses whenever you push an A button and send the signal to another Circuit Playground, it'll turn on a light animation on the other board, when you press that B button, it'll turn on a sparkle animation and each board will be able to both transmit and receive, so if you press it on another Circuit Playground then that whatever board receives it will start the animation and you can do more than just play an animation when you receive a signal, a fun project would be to make a laser tag game using multiple Circuit Playground Expresses and that'd be pretty easy to do within MakeCode as well so let's go over and see how to make this program.
So I've opened up if I open up MakeCode for Circuit Playground Express and first of all we're going to get rid of our forever loop, because we don't need it for this and we're going to use for the first time in the network section, we're going to use our on infrared received loop and we're going to use on button press. So, on button A click from an input and on button B click. So, we'll go back and network on button A click, will get the infrared send number, command and then we'll put one in button B click as well. So, on button A click right now we have infrared send number 0 and button B will change this to 1.
Now we don't send just a 0 and a 1, what's sent is 59 on-off pulses of infrared light with varying durations between each pulse and our code on the other end know to recognize that series of pulses and change it into a zero or a one when it receives it. So, there's a lot that goes on behind the scenes, as usual, MakeCode but it's easy to access for any user through this interface, so we have a transmit of button A button, B click without each send the number when it's pressed.
So, now we need to do our "receive" so we're going to get an if-else logic loop, then we'll put that in our infrared received and within true, we're going to need a comparator so, we'll take this equals and we need so when that when a signal is received, it's going to change it into the variable of number or num. So, we want to grab that variable, which is underneath variables and you can make more variables if you need them, but for this, we're only going to use this one. So, if so when it receives an infrared signal it'll save it as the variable number, so we have logic this says if the number equals 0, then we'll play an animation. So, we'll say if it's 0 then we'll play the rainbow animation for two seconds. So, if we receive an infrared signal and it's not 0, then we'll show a sparkle animation for two seconds.
Now if there are other infrared signals bouncing around we might want to have another if statement with another comparator, that says if the number equals one, then play this animation but because for this example, we only have two there are no stray infrared signals bouncing around this will still work. You may find that A button press from some different remote will initiate a sparkle, but I think that's something that we were prepared to live with here and then we want it to clear when it's all done. So we'll download that to our Circuit Playground and when it receives a signal it'll initiate the light animations, just like before and something worth noting, is that on our virtual interface we can test this code just like usual so when we hit a button and then we have a command to transmit something another Circuit Playground will appear in our interface and the animation will play on their Circuit Playground. Then on the lower one we could do the same thing hit the buttons to transmit up to the top one so again, one of the great things about MakeCode is you're able to you're able to try out your code and see it working anything on the board at least, see it working in an action before you load it to your board, so you're able to know that you've got working code before you have to mess around with hardware, which could have its own concerns like say you have the wrong type of USB cord going to it in your codes and out not loading you might think that there's something wrong with your Circuit Playground Express, when you can check that you've got working code on the computer before you even try to send it there and you'll know that there must be something else happening.
So we can also take a look at the JavaScript, of this same program that we made and again there's a lot of commands within JavaScript that make this just a lot simpler to use and I'm not going to get into it much now, I just want to remind you that it's an option to program in JavaScript as well, through MakeCode.
So that's all there is to it to make this program there's a lot of different options a lot of fun things that you can do with the transmit and receive function especially if you're say in a classroom with a lot of Circuit Playground Expresses together you can make lights sync up by using infrared signals, you can make a laser tag game, you can even make proximity sensors, using the infrared transmitter and receiver if you want to learn more about the Circuit Playground Express and everything you can do with it using MakeCode or Circuit Python, there are lots of other tutorials on it available on our tutorials page so go check them out.
Thanks
Makers love reviews as much as you do, please follow this link to review the products you have purchased.