3 Easy @Raspberry Pi Pico Projects that ANYONE can tackle!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

how is this tiny pi beeing hostet? Is there some slot board where they can be switched out?

👍︎︎ 3 👤︎︎ u/IAmStickdog 📅︎︎ Jan 31 2021 🗫︎ replies
Captions
hey everybody welcome back to print and play and in my last video we looked at this this is the raspberry pi pico it's the latest board from the raspberry pi foundation and for the first time it's not designed to be a standalone all-in-one computer rather this is a microcontroller board designed to compete with the likes of the arduino so this means it's got all sorts of general purpose input output and it's designed to be used in projects and also be very low power consumption in that video we went over the specifications on the board and then we ran some source code on it but i thought it would be neat to take the board and write some code that pretty much anybody can get behind a project that teaches you how to use buttons and leds and even a speaker as well as one of the internal components on the board and that's just what we're going to do today so if you're ready let's jump into the build and the project okay so let's go over the parts for today's build obviously we have the star of the show the raspberry pi pico we also have a breadboard we have a piezo speaker we have a switch we have two leds a 2k resistor but you can go higher or lower on the resistance depending on how bright you want your leds to be and we have eight jumper cables so let's start off by installing the pico itself so go ahead and push that into the pi next from the third pin on the top left which is a ground pin i'm going to connect my resistor to both it and to the negative terminals across the top so that now means all the pins in this spot are resisted ground through here next let's go ahead and install our piezo speaker and i install this with the positive terminal facing up and i like to put it as far on the board as possible so you can see there the positive terminal is indicated by a plus sign on the top next we'll install our leds and we're going to install them with the long leg pointing down i like to tuck these in as close to the pi as possible making sure that the legs are on opposite sides of the divide down the middle and finally we will install our switch i'm going to install the switch as far down to the bottom as possible so that it has very little to interfere with it and will have plenty of space to be able to push it next we can go ahead and start wiring things up with our jumper cables so let's go ahead and wire from the resisted ground to the top pins on both of our leds next we're going to go from one of the ground pins on the pi so we're going to count three from the bottom right and we'll run one of those to one of the sets of pins on the switch and we'll also go from that same pin the third one in from the bottom right to the negative terminal on the speaker starting next to that ground we have gp13 gp13 will be wired to the positive terminal on the speaker next to that is gp12 it will go to one of the two leds and you can decide which order you want them to be in next to that one is gp11 it'll go to the second led and finally gp10 goes to the other set of pins on the switch and with everything wired up and all we have to do is execute some code on it now to do that we're going to be using micro python and to run micro python code on the board if you haven't set it up previously we need to install the micro python uf2 file for the pico so we'll start off by pressing and holding the boot select button and with the button held we're going to plug in our usb if you've done it properly the pico is going to come up as a removable drive on your computer that you can then copy files to the next step is to copy the pico micro python file to the pico so it's basically just a drag and drop operation and once the file copy is finished the pico will reboot in python mode with your pico ready to go we'll go ahead and get into the thony ide now if you don't have the thony ide installed or if you don't have the pico micro python uf2 file i've included links to them in the description below so you can go download them and you'll be all caught up and if this is your first time in the thony id make sure that you click tools go to options and make sure that micropython for raspberry pi pico is selected in the interpreter drop down and the next step will be connecting to the actual pico so we'll click the stop slash restart backend button and we get a prompt down here which means that the pico is now listening and waiting for commands so the first code we're going to execute doesn't actually use any of the new hardware that we've installed instead it uses the built-in thermistor in the pico hardware so the pico is able to measure its own temperature and by extension the ambient temperature of the room it's in so if we take a look at the code i've written here the first thing we're going to do is import adc from machine the adc is the analog to digital converter and that will allow us to read off the pin that the thermistor is connected to in this case pin 4. we're also going to want to be able to add a delay between reads so from time we'll import sleep we create a new variable called temp center and it is pointed at adc on pin 4. from there we generate a conversion factor which is 3.3 which is the voltage that pins runs on divided by 65535 this will convert the value that comes in into a proper voltage then we get into the main loop that runs the entire time the pico is powered up after it's been executed so the first thing we do is set the current voltage to the reading value times the conversion factor next we do some math to convert that value into a temperature this is a temperature variable resistor so depending on what temperature the resistor is it will provide more or less resistance and since these values are constant we can then take the value that's brought in and figure out what the relative temperature of the thermistor is now these math values aren't for me they're spread out all over the internet i will provide links to the couple of videos that reference them down below but these conversions are also used not only on the pico but also on some arduino hardware and basically whenever you're trying to do these types of reads for temperatures so with our conversion done then the next thing we do is uh and i choose to print the value that was read in as well as the temperature and then we wait for two seconds so if we go ahead and execute this we can see that the ambient temperature in the basement is somewhere between 19 and 20 degrees now if i go ahead and put my finger over the chip and we wait just a little bit the temperature begins to increase so we've gone from 19 to 20 to 21 and that's about it i'm pretty cold myself so i wasn't expecting much of an increase if i take my finger off of the chip and we wait a little bit the temperature should start to come back down from 22 down to 21 and so on so for the next example i thought it would be neat to teach the pico to flash some lights and play some music so the first thing we need to do is import pulse with modulation or pwn and pin so pulse with modulation allows us to basically change the frequency at which pulses are being sent out over the gpio which allows us to change the tone being placed by this piezo speaker pin will allow us to be able to connect to not only the speaker but also the leds also we're going to need some delays in there so once again we'll import sleep from time so the next section of code is developing a dictionary of musical notes now a dictionary is basically what's known as a key value pair you provided a key and it returns a value so this allows me to generate notes so in this case a low b or b0 is equal to 31 so 31 is what we'll actually want to play through the speaker to create a low b we can go all the way up to a d sharp number eight which is 49.78 next we need some songs so i copied this mario song from an arduino example which i'll link to in the description below and essentially it's the notes that needs to play and then you'll also notice that there's zero so a zero basically tells it to hold the note so uh one beat of e7 and then a second e7 which is held for two beats instead of one if we look at the hot cross buns example that i've provided you'll see that we have not only holds but we also have silence so this allows there to be a beat of no audio playing coming down to the actual code while there's a play note subroutine which takes a note in duration so note as you can probably guess is the note that's being played and duration is how long to hold it for so if a note is zero do nothing if a node is s then turn off the speaker and do nothing and then otherwise we're going to turn off the speaker to create a break between the previous note and the current note pause for 0.05 seconds and then we're going to toggle the lights because i want the green and blue leds to flash back and forth as it plays music every time it changes the note then we turn the speaker on and we play the note and hold for the duration so the actual code that plays the music starts down here the subroutine ends when the tab value here returns right back to the left side of the screen so when we define a subroutine we then tab in and then everything that is essentially to the right of here is part of the subroutine so when we start the program it turns the green led on the blue led off and then it says for every note in our mario collection play the note and then finally after it's played through all those notes turn off the music so if we execute this we get a pretty passable version of super mario brothers now for hot cross buns we want to play it a little bit slower so we'll change our duration from point zero two to point zero four and then we change the mario here to hot crossed buttons and execute so essentially you can define as many songs up here as you want and then just by changing this value you can have them play through now so far all of our examples have been non-interactive you click the run button it executes some code and it does something but there's nothing to be done while you're doing it and one of the things that makes these boards fun both the pico and other boards like it is the fact that with all the gpio you shouldn't be able to actually interact with it and build projects so i thought it would be cool to build a morse code interpreter now the other thing is since with the thony ide while you're executing code and storing stuff in memory on the pi you can still interact with it it means that you can use your pi almost as an extension from your computer so let's go ahead and add the morse code creator that i've written to the pico we execute it and you can see it didn't do anything it obviously did something down here but it didn't actually cause the pie to do anything but now that we've got a prompt that we can type things into if we do play message and type in hello world and execute it we can actually play out hello world and morse code it plays it through the speaker and it flashes the led on and off as well so how does this work well much like in the music example we import pin and pulse with modulation as well as sleep the first thing we do is create a dictionary of morse code values so on the left we have the alphanumeric value and on the right hand side we have a string that indicates the way it's played out in dots and dashes so an s indicates short or a dot and an l indicates long or a dash so scrolling down we see that we have our button defined it's on pin 10 it's an input pin and it's set to be pull up this means that it's always set to high except for when you touch the ground so whenever this pin goes to ground or goes low we know that the button's been pressed next we have our two leds defined as short led and long led those are defined on pin 11 and 12 and their output pins and i'll go into a little bit more of the explanation of why there's two leds shortly finally we have our speaker which is a pulse width modulation on pin 13. we have our the speed of our dots and dashes defined here 0.1 for fast and 0.2 for slow and we have variables that allow us to turn sound and light off as well as change the pitch and volume so since we can interact with it if we set our sound to false and then we say play message hello we get morse code played through the led but no sound effect if we then set sound back to true but we set light to false and execute the same code again we get sound but no flashing lights of course you could set sound and light to false but then basically nothing would happen we can also change the pitch so if we set pitch instead of at 600 we set it to 1600 and we execute the same code we get morse code for hello plate again but a much higher pitch and volume as you might guess also affects the volume so what's happening in the background well we're executing play message play message accepts message as a string and then it goes through every letter in that string or for c in message and it sends that letter as a lowercase character to blink letter so what does blink letter do well we can see if the letter is not equal to nothing then it looks up the morse code value for that letter and stores it in current letter if letter is equal to a space we pause for 0.6 milliseconds but we don't turn on the light or beat the speaker as it goes through it prints out through console the letter as well as the value it found for that letter in morse code then it uses that morse code definition and goes through each character which is either short or long and if it finds a long one it sets blink speed to slow if it finds a short one it sets it to fast and then if light is enabled it turns the lights on and off if speaker is enabled it turns the speaker on and off and then it pauses for the appropriate delay and that's it by turning the speaker on and off and turning the lights on and off and using that dictionary to look up values we can put out pretty much any message in morse code that we want but what if we wanted this to change morse code into text instead of text into morse code well then we can simply type in a record message execute it and it goes into recording mode then we can use the button to put in our dots and dashes and on the screen it actually converts it to text for us so you can see i put in three short then three long then three short and that converted to sos so how does that work well we're going to take a look at the record message command and you see the first thing it does is put out a message letting you know that you're in morse code message mode and if you wait 5 seconds it will exit then we set a bunch of our variables so we have a time counter a delay counter and then we also keep track of the current letter in the current word and we always need to keep track of the previous status of the button so that we know if the button's just been pressed or just been released then we run a loop so while true creates an endless loop unless you break the loop somewhere else it'll just run forever and ever so if the button is being pressed we set our delay count to zero and we begin increasing the time count time count allows us to track whether or not you've pushed a short or a long so if the time count is less than or equal to 15 we're still in a short press so we light up the led that indicates you're pressing a short note we turn off the long led but if we get over 15 well then that short led gets turned off and the long led gets turned high so by having two leds it allows you to train yourself on how long you can push the button before it goes between the two modes also if the button has just been pressed well we need to turn on the speaker so that it's playing a tone if the button's not being pressed we need to turn the speaker off as well as both leds and then we just take a look at the time count and again if it was less than or equal to 15 we're going to add a short to the current letter and if it was greater than 15 we're going to add along to the current letter then we're going to increase the delay count plus 1. this allows us to track how long it's been since the last time the button was pressed if delay count gets above 60 or 0.6 of a second then we check to see if there's anything stored in current letter if there is then we're going to add that current letter to the current word and we do that by doing a lookup so we pass current letter in its morse code value to the letter lookup which basically goes through all the entries inside the dictionary and tries to find one that matches if it does it'll return a letter and we add that letter to the current word then the current letter gets set to nothing and we print what the current output is if there's a delay of 300 or 3 seconds we add a space to the current word and if the delay gets to 500 or 5 seconds then we exit recording mode and it's just that simple and these values can all be adjusted to your comfort level so that you can get used to it and you can increase them or decrease them depending on your proficiency i hope you guys enjoyed this look at three pretty quick projects that i think anybody with a pico can tackle if you end up using the source code for any of these in one of your projects let me know what you used it for you guys are free to do with it whatever you like if you like this video don't forget to click the like button so i know that you guys want to see more pico content in the future if you're new here don't forget to subscribe so that you're amongst the first to see my videos whenever they go live if you have another video idea or another project you'd like to see me tackle in the future let me know in the comments below i would love to give it a shot alright well that's it for today's video but until the next one stay creative
Info
Channel: Print 'N Play
Views: 67,899
Rating: undefined out of 5
Keywords: Raspberry, Pi, Pico, Raspberry Pi, Raspberry Pi Pico, Thonny, MicroPython, Morse Code, Music, DIY, Programming
Id: k2thJQxO17w
Channel Id: undefined
Length: 19min 21sec (1161 seconds)
Published: Sun Jan 31 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.