Raspberry Pi Pico Voltmeter - UART serial with the Pico and GUI application in C/C++ and MicroPython

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello in this video i'm going to show you how you can use a raspberry pi and a raspberry pi picot together this will use features of each including the analog to digital converter on the pico and the graphical interface of the raspberry pi to create a simple volt meter this is going to be different than i originally planned i was hoping to use i squared c or possibly spi to communicate between the two but i ended up using new art serial instead that's not a problem with this example but i'll explain the reason for that in the video i hope to look at the other protocols in a future video i've created two versions of the code on the pico one in micro python and one in c stroke c plus plus as a bit of a comparison i'll explain the reason i decided to use the uart first and an explanation of some of the issues i had trying to use the other protocols i'll also explain some of the setup needed for micropython and c if you're just interested in the practical side then look for chapters on the timeline or in the description to jump to the relevance section i've already created another video comparing the features of the raspberry pi and the pico in that video i said there are advantages of using both together and this will show one example why this is the case it's not the only way of achieving this and arguably there are other components that would be better suited but this does provide a good demonstration of how the two can work together like most micro controllers it's possible to program the raspberry pi pico in c and c plus plus but it also supports micro python which is a version of python specifically for microcontrollers i think the inclusion of micropython is going to make microcontrollers much more accessible to those that don't want to program and see or aren't quite ready to learn the c language it's also very useful to be able to use a similar programming language on both the pi and the picot and python has some advantages when creating graphical applications on the other hand c is a very powerful programming language it can be difficult to master it was one of the first programming languages i learned there isn't one i'd normally recommend for beginners to programming it is very powerful and is sometimes the best choice perhaps the most difficult part i encountered with c is the lack of feedback for debugging when my code didn't work there's some ways to work around that but i did get there eventually i'll be showing both micro python and c on the pico there are a few different serial protocols which can be used for sending information between a computer and a microcontroller i was hoping to use either i squared c or spi for communications between the raspberry pi and the picot i wanted the raspberry pi to be the primary controller sending the instructions to the pico as a secondary device there is lots of really good documentation on the pico and i started by looking at the different protocols and how well they are implemented or at least documented whilst there is good documentation for both i squared c and spi where the picot is the primary controller the documentation does not explain how to use it as a secondary device simply invented in hardware but it appears it's not a standard feature of the micro python language some boards such as the pie board have specific libraries that do support that but they're in a hardware specific way i was not able to find anything specific for the pico although i've not given up on them i've decided to stick to something a bit simpler for now so instead decided to use the uart the c programming language does sound a bit more promising for i squared c and micro python but that is something i can look into at another time to set up micropython on the picot then you first need to download the micropython uf2 file and copy that to the pico i've covered this in my earlier video so if you haven't yet created your first micropython program then you can take a look at that video which will be linked in the description one thing i found was that the repel the read evaluate print loop is available on uart0 this means i was not able to use that for the communications but there are two uarts on the pico and so i used uart 1 instead to use the c programming language you first need to install the c tool chain if you're using raspberry pi the easiest way is to run the script which installs everything needed i then created a directory pico projects inside the pico directory i did this outside of the pico folder at first but because of the way the libraries are set up i found it much easier to put that within the pico folder that is created from the install script you should copy the pico sdk import.cmake file from the picoexamples folder and also copy and modify the cmakelists.txt file you also need your c source code file which i'll explain once we get it to the project then to compile the program first create a directory called build cd build and run cmake double dot which will run the cmake program then make minus j2 i used the j2 option as i'm running on the raspberry pi 2 and so this will use two threads for compiling the code this creates a dot uf2 file which can be copied onto the picot and then we'll run that's the setup out of the way now time to explain the project this will be using the adc and logged in digital converter on the pico this converts an analog voltage into a digital signal that we can use in the code it's a fairly basic adc inside the pico but it's sufficient for a basic voltmeter that we'll use here it only supports up to 3.3 volts which is the internal power of the pico you can take the 3.3 volts from pin 36 do not use v-sys or v-bus which are at five volts the adc is available as analog zero to two on pins gp26 gp27 gp28 and as analog 4 on an internal temperature sensor inside the pico you may have noticed i missed out analog 3 which is connected to gp29 of the microcontroller that's not available on any of the pins on the picot the documentation says it can be used to measure the vsys voltage but gives a much lower voltage so i expect it needs a different conversion factor you can still access that from the pico code but i'm not used in the gui due to the potential confusion note there is only one adc on the pico so it's only possible to use one at a time although you can switch between each of the different ports so you could quickly flick through each of the different ports to get the different values the raspberry pi and pico both work at 3.3 volts so it's just a case of running the appropriate wires between the two first the ground is connected this grows from physical pin 14 on the raspberry pi to physical pin 8 on the picot next connect the transmit of the raspberry pi which is physical pin 8 to pin 7 of the picot which is the receive pin finally connect the receive pin of the raspberry pi on physical pin 10 to physical pin 6 on the picot which is the transmit on the pica you will notice that the transmitter received lines are crossed so the transmit on one device goes to receive on the other and vice versa in this case i'm going to send messages between the raspberry pi and pico using ascii text i want to send the data from the raspberry pi to the pico specifying which of the ports to monitor sending 0 for analog input zero to four for analog input four then the p co responds with the string pin followed by the pin number eg zero for analog pin zero then the value followed by a floating point number of the voltage at that pin note that these are all sent as ascii strings including the pin numbers i created the code for the pico first this allowed me to test the communications using minicom to monitor the serial connection prior to creating the graphical application this version is in micropython it's created using the thoni editor which could communicate directly with the pico you can see that where it's connected down here the code starts by importing the uart and adc objects from the machine module then sets up a uart object and some variables for the analog to digital values these are set to initial values so it doesn't give an error later the adc object is for the analog to digital converter the conversion factor will be used later to convert the digital number to a voltage value the rest of the code is in the while loop this reads in a single character from the uart which should be a single digit as an ascii character it's in the range 0 to 4 using the ascii characters then it sets that as the pin to monitor reads in the value and then writes that out on the uart as long as micropython is installed on the pico already you can just run this using the run button or to have it run automatically when the picot is powered on save it as a file called main.py onto the pico this is the version of code created in c it's shown here using the visual studio code editor unlike the micropython code which just weights on the serial connection this code is based on interrupts but that shouldn't make it too difficult to understand the code starts with the header files needed note that these need to be listed in the cmakelists.txt file as well which is shown here as well as the standard library there are headed files for the uart interrupts analog to digital converter and the standard input output used for the string handling then defines the parameters for the uart along with the pins to use as you can see here this is for uart one as well this is then the default pin and the conversion factor used to convert the digital reading from the analog to digital converter into a value between 0 and 3.3 to represent the reference voltage i'll skip past the interrupt handler for now i'll come back to that we'll go to the main function which is listed here and this sets up the analog to digital converter first and then the uart it starts with a very slow baud rate but then that's set later and it turns the relevant pins into the appropriate mode for using the uart and then towards the bottom of the code this is where the interrupt handler is set up and it sets it up as a function called on uart rx now can we show you that and then at the end it just goes into this while loop that just keeps running so if we scroll back up to the interrupt handler code whenever it gets a signal on the uart then it will run this code it looks for it being readable and gets that value and stores it in this chair variable it checks that it's a valid number and if so then sets the appropriate pin and then it creates it reads the analog to digital converter using the adc read and then creates this string which is the same format as the string as we used before on the micropython version it uses this s printf command to generate that and then as long as the uart is writable it puts that back on the uart to send back to the raspberry pi and every time there's a new instruction comes from the raspberry pi it will run this code so to compile that i'll show you how we do it on the command line so it's in the pico folder and i've called the folder pico projects and then inside that i've created voltmeter and you can see here they see make and the voltmeter.c file then i created this build directory and run cmake on the previous level and then make and this will build up and create the file that we need is this voltmeter.uf2 file and you just restart the pico in the with the boot select button press down so it mounts to the file system and then you can drag this into it now one thing you may notice is that the code on the c version is quite a bit longer than the micropython version in fact it's over three times longer so that's one of the disadvantages of using z but it does give you a lot of control of what you do the python code for the raspberry pi is written for pi game zero i'm showing here in the mu editor as this has a pi games zero mode which makes it easy to create by game zero code as you can see it's in mode pi game zero so first the code imports some modules serial time and the math floor method which we'll be using later it sets the screen dimensions and this will put it into the the guru mode sets up the serial connection it sets up an actor which is a image of the picot and then creates some variables for storing the the pin that we're showing in the voltage and then it creates more actors representing each of the pins and they'll show as enabled which is a green circle or disable which is a red circle and you'll notice that i haven't used the one that says gp29 and i've put that off the screen so you can't see it but it's there if you want to add it yourself later just move it to another position there's some variables here to add a delay in how quickly we send commands i'll show that a bit later so the draw function here basically is going to draw the screen clear the screen gives it a color draws the pico image and the pin images and then write some text for the text you'll see that i've used segment seven standard as a font which is a font i've downloaded this for the dot for the decimal point uses a whole character which just didn't work so what i've done is i've split the value that i get into a whole number part which is this one here which is first displayed and then the fraction part which is displayed afterwards and they overlap so that the there isn't a big gap between them and then the update function is used to send the commands to and from the pico and to interpret those in pi game zero then the update function and the draw function is called roughly about every 60 times per second and if you tried to send a command to the raspberry pi every 60 seconds then that's going to be putting a lot of load on it but more so it's you're not going to be able to see the value as it's displayed on the screen because that value is going to be changing far too quickly so this is just to slow it down so it's just a little counter that slows it down so uh only so that this the rest of the code basically runs twice a second so a lot slower than it would run otherwise it sends a signal a value to the pico um which is the digit of the pin the analog pin it sends it as an ascii character so this 48 is the ascii number for the zero character so it adds that and although we're only sending one byte here the serial right command needs this byte order to be included so it's just included but as it's only one byte it doesn't matter that's that's whether it sends the most significant part or the least significant part first then read the response back from the pico uh decode that and then set the the voltage based on the value that we get back it looks for splits this string up into the relevant parts and then that voltage is what the draw function used to write the value to the screen and then finally we've got this on mouse down function which is used when the mouse button is pressed down and this is used to select the appropriate pin so if you click on one of the pin images it uses glide point if it glides when the button is pressed down then it sets that one as the one that's enabled you can now see the program working by clicking on the different pins they will be able to show the voltage at those pins i've created analog 0 to 0 volts analog 2 to the 3.3 volt rail and then analog 1 is connected to the potentiometer so you can see the value change as i should adjust the dial if i turn it anticlockwise then the voltage will reduce if i turn it clockwise then the voltage will increase there are lots of opportunities for improving the code one possible improvement would be to add a voltage divider using resistors between the signal and the input of the p cone this could be used to increase the maximum voltage you're able to measure if you do use this then you need to ensure you don't put more than 3.3 volts into the picot as that could damage it you'll also need to change the conversion factor used in the code to display the correct value another thing you may want to change is that the temperature adc value still shows the value as a voltage that could be changed to a temperature instead last but certainly not least it could do with some better error handling the pico code isn't too bad and then it will just ignore any unexpected values however the raspberry pi code could do with some better error handling as if the pico isn't connected or it gives an unknown value then it will cause an error and cause the syst the program to crash so that's something you may want to have a go at if you do create any improvements to the code then please share them and let us know in the comments that's this project finished for now this is shown how to create micropython and c code for the pico which includes how to measure a voltage using the built-in analog to digital converter it's also covered how to transfer data to and from a pico using the uart and then how to display that data on a gui using pygame0 i hope you found this useful i'm planning to create some more projects using the raspberry pi the pico and other microcontrollers in the future so if you haven't already subscribed please do so and click on the bell icon to get notified of the videos when i upload them i hope to see you again on a future video
Info
Channel: Penguin Tutor
Views: 31,441
Rating: undefined out of 5
Keywords: raspberry, pi, pico, raspberry pi, microcontroller, adc, analog, digital, converter, convertor, uart, serial, communication, spi, i2c, cpp, c++, c/c++, c programming, c code, program, programming, electronics, voltage, measure, voltmeter, current, power, resistor, potentiometer, divider, master, slave, primary, secondary, usb, micropython, python, micro, pygame, zero, pgzero, pygamezero, pgzrun, code, visual, studio, thonny, mu, mu editor, editor, cmake, make, compile, linux, os, computer, gui, graphical, user, interface
Id: egOOW0WstRg
Channel Id: undefined
Length: 23min 12sec (1392 seconds)
Published: Mon Feb 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.