Talk to Your Pico Over Serial | Raspberry Pi Pico UART Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what is up guys today i'll be showing you how to write code to use serial input and output on the raspberry pi pico by the end of this video you'll be able to write a program like this that lets you see the output from your pico as well as send it commands over serial to reset it enable and disable pins and more getting readable output from your code is an extremely important part of debugging without an idea of what state your program is in you might as well be trying to read the mind of your microcontroller which can be frustrating instead to communicate with the pico we're going to use an ftdi rs232 cable like this one rs-232 is a physical layer protocol responsible for carrying the uart protocol or universal asynchronous receiving transmit we're going to hook up our ftdi cable to the uart zero bus on our pico uart0 is the default bus used for standard input and output or standard io when we write code calls to functions like printf scanf etc use this bus the way we're going to wire our ftdi cable to the pico is as follows the orange cable or host receive is going to go to pin 2 the yellow cable or host transmit is going to go to pin 1 and then finally the black cable for ground will go to pin 3. having a common ground between devices is extremely important without it it's likely that your devices won't be able to understand what the other one is saying so it's important to not skip this step here you can see i have my pico wire the same way the ftdi cable is plugged into the uart zero bus for serial communications and the micro usb cable is plugged in so that i can program the device on my linux workstation when i plug in the ftdi usb cable it populates as dev tty usb0 to communicate with it we need to use a program able to open serial devices i use minicom to install it type sudo apt install minicom once it's installed run sudo minicom capital d slash dev tty usb0 once inside minicom need to press control plus a then o to go to the options menu select serial port setup then press f and this disables hardware flow control so you can type to the pico alright now that we have the serial port hooked up let's write some code by the way i'll be posting all this code onto the channel's github check the link in the description for that also i noticed that 90 of my viewers are not subscribed if you're not subscribed right now do me a favor hit that sub button i'd really appreciate it step one we need to confirm that we can actually get output from the device remember like i said all standard output and input goes to the uart terminal we have hooked up so we can literally just type printf in our code and we in theory should get output to the terminal let's try that real quick so first we need to edit all the standard i o and then we can type printf code and then we're going to sleep a little bit so we're not blowing up the terminal so let's make that and again we're using the build environment here that i've done in previous tutorials if you haven't seen that make sure you go watch it okay we make it good and then we need to copy our uf2 file to the raspberry pi awesome so now every half a second we get output to the serial terminal great that means that our terminal is working and we can get data from the raspberry pi pico now let's send data to it so just like we're doing printf a standard libc function we can actually use functions like scanf or gets or otherwise to read data from us so for this one what we're going to do is we're going to create a character buffer let's call it buffer and we are going to use scanf so scanf for those of you that don't know is a function that reads in data from the user in the libsy library and it takes a format string parameter the format string is going to be 1024s which means it's a 1024 byte long string and we're going to store that output into the buffer and then we are going to use printf to just print it back to us and that'll be our confirmation that we can actually get data into the raspberry pi pico so same thing we'll make our code the code builds successfully that's awesome so we'll copy our code in the device should reset and then if we type some data we hit enter it should show up and it does so when i'm typing i'm hitting enter and it's coming back to the screen the problem with this is that you're not seeing the return output from your typed keystrokes but when i type and i hit enter it's coming back to the screen by printf great so now we can do input and output so let's do something fun right let's set it up so that when we send it data it checks what the data says and then based on what it says it does something so we'll do a program that turns the led on and off based on our input so basically we already have the input coming in we can do a stir compare of our buffer to some value so we'll say if our input buffer is equal to on and that's this line does if the stir compare between buffer and on is zero which means they're the same we'll do a gpio put to the led pin and turn it on otherwise if the stir compare of our buffer is equal to off then we'll turn it off and i have to add some extra stuff up here just to initialize the pin so a gpo io init led pin has to happen first and then gpio set direction led pin gpio out it's not actually direction it's der and then obviously we need to pound to find what does led pin mean led pin has been 25 so this will basically read in our input if the input is on it'll turn the pin on if the input is off we'll turn the pin off let's try that copy the file over okay you should be able to type data and get it back so if i type on enter the light comes on if i type if i type off enter light goes off the problem here is i deleted the printf so you can't see what i'm actually typing but i promise you i'm typing on light goes on i'm typing off light goes off pretty cool and then one more thing we can do we can add another if else statement to this stir compare and if it says reboot if it says reboot we can do a reset usb boot and what this will actually do is put the raspberry pi pico into its programming mode so i can tell it to reboot and that will enable me to reprogram it without me having to unplug it oh so to use the reset boot function we need to actually add something and now actually we're also going to add a printf here that'll output our input so we can see what i'm typing we'll remake this real quick okay and then we're going to copy it over to the device okay copy this so i'm typing on light goes on typing off the light goes off and then we reboot it and we now have put the device back into its programmable mode anyway guys this is serial i o on the raspberry pi pico i hope you enjoyed this i hope you learned something if you did hit like hit subscribe drop a comment and i'll see you guys in the next video bye-bye
Info
Channel: Low Level Learning
Views: 58,539
Rating: undefined out of 5
Keywords: raspberry pi pico, raspberry pico uart, raspberry pi pico programming, raspberry pi, uart tutorial, rs232 tutorial, embedded programming tutorial
Id: pbWhoJdYA1s
Channel Id: undefined
Length: 7min 58sec (478 seconds)
Published: Sat Mar 06 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.