Raspberry Pi PICO | Starting With MicroPython + Examples | I2C OLED, ADC, PWM

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
there's a new microcontroller on the market and this time it comes from raspberry pi this is the new 2020 raspberry pi pico now we all know raspberry pi being more of a computer than a microcontroller but now with this new product we have a programmable microcontroller just as an arduino nano for example but the pico has a lot more speed more memory it is a dual core microcontroller and best of all it is a 32-bit arm controller compared with the 8-bit of the arduino nano so imagine the power in this video we will start programming this microcontroller in micro python and in future episodes we also start working with this in c plus plus i'll show you everything about this new board compare it with the arduino install the coding software and show you some examples from simple ones of an led blinking to adc read and ice crazy communications just as we do with an arduino so guys let's get started [Music] this video is sponsored by pcbway go and check all our services for pcb prototype from one up to 14 layers order the smd stencil which is used to solder smd components the pcb assembly where they will solder all the tiny components for you automatically using machines also flexible pcbs up to 8 layers and their new services of 3d printing injection molding laser metal cutting and cnc milling of any 2d part of different metals so code now on pcbway.com upload the gerber files adjust the settings and for only five dollars you can get up to 10 pcbs of 10 by 10 centimeters what's up my friends welcome back so this is the new raspberry pi pico so let's start with some specs the board is using the rp 2040 microcontroller designed by raspberry pi this is an arm cortex m0 dual core processor with flexible clock speed up to 133 megahertz 133 megahertz compared with the nano for example that works at only 16 megahertz so imagine the speed difference on side of that this is a 32-bit microcontroller so the data flow would be four times or more even faster compared with the arduino boards which is an 8-bit controller so for now we have dual core 133 megahertz and 32 bit structure compared with a single core 16 megahertz and only 8b structure of the atmega 328 of the arduino the chip has 264 kilobits of sram memory and 2 megabytes of flash memory and it also has an ultra low power sleep mode and by the way this board operates at 3.3 volts and not 5 volts as the arduino now as for the pin out this board has 40 pins and from this 26 are multi-function gpio ports it also has two spi ports and twice square c ports and as you can see this could be programmed to be connected at any pin it also has two uart ports and three adcs this adc are 12 bits which is four times better than the 10 bits adc of the arduino it also has 16 controllable pwm channels which we'll test in a moment as an extra this ic has an on-board temperature sensor which we'll also test later accelerated float libraries and 8 programmable pio which i never used so let's start this microcontroller could be programmed in c plus plus which is pretty close to what the arduino is using but it could also be programmed in micropython which is a compact version of the python made for small microcontrollers so today we will work in micropython and on future episodes we will start with c plus plus to start with micro python we have three steps first we install tony which is a programming environment that i will use we add micro python to the board and then we see how to upload codes go to tani.org and download the software in my case i'm using windows so i will download this one install the software and this will take just a couple of minutes now you can run tony at this moment connect your pico board to the pc using the usb cable so first thing first we go to tools and select options and here in the interpreter tab you select micro python raspberry pi pico you can also select the com of your pico board and click ok if you don't already have micro python running on your pico when you click the stop or the play buttons a prompt will automatically flash micro python on the pico if this process doesn't work we go to this website we click on the getting started and here we download the micropython uf2 file once you have this file downloaded you have to disconnect the pico from the pc and now you press the boot button while pressing this button you can connect back the picot to the usb cable you will see that now it will appear on the pc as a new drive all you have to do is to copy the uf2 file that you have just downloaded to this drive and you will see that that will automatically disconnect and reboot so now micro python is installed so connect back the pico and run tony now click the stop button and as you can see we are connected to test if the connection works in the shell tab you can type something like print hello world and press enter if hello world is printed then we are good to go so let's start with the code example let's make the most basic and blink an led the pcb has an onboard led that is connected to gpio25 so now create a new project and save it to your computer let's give it a name such as blink.pi for example in order to use the pins we import the pin from machine and in order to create a delay we use timer i define my led as a pin for the gpio25 and define it as an output i also create a boolean led state that is equal to true i create my timer to blink each one hertz so it will blink each one second now each time this timer ticks i will invert the led state and with the led.value i write that value to the led where true is led on and false is the led off press the play button and the led is now blinking so that was pretty easy here i have another code that will blink two leds at the same time one on gpio2 and another one let's say on gp20 as you can see this time i'm toggling the leds in an alternate mode but wait a minute if i disconnect the usb cable and plug it back in as you can see the led is not blinking anymore well that's because we are running the pi code from the pc if you want your code to automatically run when you power the raspberry pico you need to add that code to the microcontroller so here is how let's create a new file click the save button and here select to save it to the raspberry pico now it's very important to name this file main.py if you give it a different name it won't run automatically because the pi is always looking for the main.p file so now we can add our code when i click the save button the changes are saved to the main.p file that's on the microcontroller memory so as you can see the led is blinking but now i remove the usb cable and connect it back on and there you go the led is blinking automatically without me running it from the pc okay so now let's see more examples how about using the adcs we have 12-bit adcs on the gp26 27 and 28. i define my adc input connected at pin 28. to create the infinite loop i make a while true and here all i do is to read the adc value with the redux16 function then using print i can show the value on the shell monitor i have a potentiometer that is connected to the gp28 pin as you can see i rotate the potentiometer and the value goes from 0 to 65536 and that's weird because this is supposed to be a 12 bit abc and not 16. anyway that's how we can read analog values with this adc on the raspberry pico now remember that the pico has an onboard temperature sensor and that's connected on pin 4 and it has an analog output so now if i make the same code but instead of using pin 28 i read the pin 4 and then i multiply this by the temperature multiplier i get the temperature as you can see i run this code and when i hit the pcb the temperature rises now let's start with some iscsi we import from machine the i square c and the pin modules to start an sq port we use this line and we specify on which pins we want the sda and the scl which in this case i place them on pins 8 and 9. using the i square c dot scan function we can see if we have a nice crazy device connected i connect my lcd to pins 8 and 9 for sda and scl i run this code and as you can see on the monitor i get that i found an acecracy address which in this case is 0x27 and that's correct i now connect this ice creasy oled display and run the same code this time i found the address 0x3c and that's also correct so let's see how to control this oled display using iscsi communication i connect ground and 3.3 volts from the pico and the sda and the scl pins to the gp8 and 9. now in the code i need to use the ssd 1306 module so i import that i run this code and as you can see i get an error there is no ssd 1306 module and that's correct because i don't have it we have to add that to the pi as well so i found this module on the internet ready to use so back in tony create a new file save this file and select to save it on the raspberry pi pico give this file the name ssd1306.by and click save if we now go to open and select the pico memory as you can see we have that file on the raspberry pi memory now go to the online code which you can find below and copy this code then we paste this to my file click the save button once again now go back to the main code and run it again and there we go we have no more errors because now we have the 1306 library on the memory so now we can use that in the code i start a new sqc communication on those pins i start the oled display with this function from the used library with the fill function we can set all the pixels to be on or off by adding a zero or a one so to clear the entire display i make only that field zero with the oled.txt function we can send text so add the text the column and the row and finally with the oled.show we send the data to the display run the code and there we go i have electronoops on my display okay so this time is the same code but i've added the raspberry pi logo in hexadecimal file we create it with this buffer and send it to the display on column 96 and row 0. and as you can see we now have that logo as well on the display in the same way you can use this other display which has the same chip but a different size now we could merge the adc example with this oled example so all i have to do in the code is to take this adc read and pass it to a string using the srt declaration i also add the round function so it won't print all the decimals only two to make the screen look cooler i also use the invert function so the text will be black and this is the result i now have the adc read from the potentiometer on the display nice right finally i have one more example the pwm control remember that we have 16 pins that could create a pwm signal so in the code i import the pin and the pwm from the machine module i also import the u-time for creating delays i attach the pwm to the pin 25 which is the pin of the on-board led i can set the frequency to 1000 hertz with the while true i can create my infinite loop and inside here i can create two more wells and this will increase and decrease the pwm value with pwm.duty i can write that value to a pin and that's it i run this code and as you can see we can now fade on and off the led quite easy right on the oscilloscope we can see the pwm signal increasing and decreasing at a constant frequency of one kilohertz and here is me pushing a static pwm signal up to nine megahertz but as you can see at this speed the shape is not square anymore but anyway a 9 megahertz signal that's a lot of speed and programming this in c plus plus i think that we can get even more out of it in a future episode i would like to share with you more examples and later start using this board with z plus plus programming this in c plus plus is not that easy to start because we have multiple configurations that you must make before starting your codes and each time you want to flash a new code in c bus plus so now you know the basics about the raspberry pi pico how to install and use tony how to permanently upload the codes to the pi in the main file and how to run a few examples for blink pwm fade ice creasy oled display and adc read i hope that you liked this video and if so give me a like thanks again and see you later guys hey guys so that's the end of this video and i hope that you like it and the most important part is that you have learned something new and as you know part of the support for this channel comes from patreon so i would like to really thank you thank you very much i'll put the list for all my patrons here and if you're not a patreon and you like to support me you have all the links below and also if you share this video give it a like or comment below that will help me to beat this youtube algorithm so thank you very much and see you in the next video
Info
Channel: Electronoobs
Views: 244,942
Rating: undefined out of 5
Keywords: raspberry pi pico, C++, thonny, micropython, REPL, programming, tutorial, i2c, OLED, PWM, ADC, windows, display, library, example, raspberry, adafruit
Id: zlKJ5hvfs6s
Channel Id: undefined
Length: 15min 41sec (941 seconds)
Published: Sun Feb 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.