0% found this document useful (0 votes)
40 views21 pages

Iniciandoconpico

Uploaded by

Control Pcym
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
40 views21 pages

Iniciandoconpico

Uploaded by

Control Pcym
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 21
218923, 07:24 Gating started with Raspberry Pi Pleo & Projects Getting started with Raspberry Pi Pico How to start programming you Raspberry Pi Pico with Thonny and MicroPython Step1 Introduction In this project, you will connect a Raspberry Pi Pico to another computer and learn how to program it using MicroPython, A Raspberry Pi Pico is a low-cost micracontroller device. Microcontrollers are tiny computers, but they tend to lack large volume storage and peripheral devices that you can plug in (for example, keyboards or monitors) ‘A Raspberry Pi Pico has GPIO pins, much like @ Raspberry Pi computer, which means it can be used to control and receive input from a variety of electronic devices. The new Introduction to Raspberry Pi Pico path (https://projects.raspberrypi.org/en/pathways/pico-intro) uses the picozero (httns://picozera,readthedocs jo/en/latest/) package to engage in some creative physical computing projects. ‘What you will make You will connect a Raspberry Pi Pico to your computer, install the Thonny Python IDE, and write a MicroPython program to blink the onboard LED. If you have additional components available, then you can also try out some more examples. hitpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-pleoiprint 421 218923, 07:24 Geting started with Raspberry Pi Pleo + ARaspberry Pi Pico with soldered headers + Acomputer that can run the Thonny IDE and program a Raspberry Pi Pico + Amicro USB cable + Aselection of electronics components, such as a button, an LED with appropriate resistor, and a potentiometer (optional) + breadboard and M-M jumper leads for connecting additional components (optional) + An external SV micro USB power source (optional) Software The project will guide you through the installation of: * MicroPython firmware for Raspberry Pi Pico » The Thonny Python IDE © wat youwiniesen + How to load the MicraPython firmware onto 2 Raspberry Pi Pico + How to program a Raspberry Pi Pico using MicroPython + How to connect additional components to @ Raspberry Pi Pico and write MicroPython programs to interact with them ©} Asaitionat information for educators If you are completing this project in a schoo! or other setting with a managed network, then you should make sure that you have the appropriate permissions to mount a USB drive and install software. Ifyou need to print this project, please use the printer-friendly version (https://projects.raspberrypi.org/en/pro iects/getting-started-wit ico/print). Here is a link to the completed scripts for this project (https://rpf.io/p/en/getting, 2D. tarted-with-the-pico-ge hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint 22 218923, 07:24 Geting started with Raspberry Pi Pleo Step2 Meet Raspberry Pi Pico This is @ Raspberry Pi Pico. Hopefully your device has already had the header pins soldered on, but if not, you might like to have a look at our Getting started with soldering resource (https://projects.raspberrypi.org/en/projects/gett ng-started-with-soldering), Ifyou have a breadboard, put your Raspberry Pi Pico on the board. Place it so that the two headers are separated by the ravine in the middle. Plug your micro USB cable into the port on the left-hand side of the board, hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint at 218923, 07:24 Geting started with Raspberry Pi Pleo Ifyou need to know the pin numbers for a Raspberry Pi Pico, you can refer to the following diagram. hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint 2118123, 0728 Getting started with Raspberry Pi Pico Step 3 Install Thonny In this step, you will install Thonny or make sure you have the l Pico and run some simple Python code using the Shell fest version. Then you will connect to a Raspberry Pi + Thonny is already installed on Raspberry Pi OS, but may need to be updated to the latest version + Open a terminal window. either by clicking the icon in the top left-hand corner of the screen or by pressing the ClrltAlt+T keys at the same time + In the window, type the following to update your 0S and Thonny + On Windows, macOS, and Linux, you can install the latest Thonny IDE or update an existing version + In a web browser, navigate to thonny.org (https://thonny.org/) + Inthe top right-hand comer of the browser window, you will see download links for Windows and macOS, and instructions for Linux + Download the relevant files and run them to install Thonny Download version 3.3.2 for Windows + Mac + Linux NB! Windows installer is signed with new identity and you may receive ‘awwarning dialog from Defender until it gains more reputation. Just click "More info" and "Run anyway”. hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint rat 218923, 07:24 Geting started with Raspberry Pi Pleo Open Thonny from your application launcher. it should look something like this: You can use Thonny to write standard Python code. Type the following in the main window, and then click the Run button (you will be asked to save the file) prine¢*Helt0 wort") You're now ready to move on to the next step and connect your Raspberry Pi Pico, hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint 218923, 07:24 Geting started with Raspberry Pi Pleo Step 4 Add the MicroPython firmware Ifyou have never used MicroPython on your Raspberry P! Pico, you will need to add the MicroPython firmware, Find the BOOTSEL button on your Raspberry Pi Pico. Press the BOOTSEL button and hold it while you connect the other end of the micro USB cable to your computer. A Raspberry Pi is shown in the image below. out the same applies to any computer. This puts your Raspberry Pi Pico into USB mass storage device mode. hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint m1 23123, 07:24 Geting started with Raspberry Pi Pleo In the bottom right-hand corner of the Thonny window, you will see the version of Python that you are v currently using. Click on the Python version and choose ‘MicroPython (Raspberry Pi Pico): “Thesame interpreter ich une Thanny idetaut) Aernative Python 3 interpreter or vita eionment Ifyou don't see this option, then check that you have plugged in your Raspberry Pi Pico. ‘A dialog box will op up to install the latest version of the MicroPython firmware on your Raspberry PiPico. | Click the Install button to copy the firmware to your Raspberry Pi Pico. Th rst hot thon mere iio * Veontobenates sab 200640 Toyedeiemede tegteny PRD Wait for the installation to complete and click Close. ©} Fimvare instalation menu You can also access the firmware installation menu if you click on ‘MicroPython (Raspberry P! Pico)’ in the status bar and choose ‘Configure interpreter .. hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint 218923, 07:24 Geting started with Raspberry Pi Pleo The interpreter settings will open. lee [raat A | Tan |S at MoxPyeen Pater Ps) Click on Install or update firmware, You willbe prompted to plug in your Raspberry Pi Pico while you hold the BOOTSEL button. He you an int out enn Fm Rasa Pico | ug nyurPo te ing De BOOTSEL ban 2 Wat deena apes 4k ta _shan te pcs nes yourP wil be ening telat vesen of IMaeeybon Gosetnedabg and sur pogamn Von tobe ntes zo2r206 (20201247) Tage dee ern: rai P2 Then you can click install hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint rat 23123, 07:24 Geting started with Raspberry Pi Pleo eoete tte 202001269 Tanna? tance aber A RF2 Saeeen Ge) Wait for the installation to complete and click Close. ‘You don't need to uodate the firmware every time you use your Raspberry Pi Pico. Next time, you can just plug it into your computer without pressing the BOOTSEL button. hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint 218923, 07:24 Geting started with Raspberry Pi Pleo Step5 Use the Shell In this step, you will use the Thonny Shell to run some simple Python code on your Raspberry Pi Pico. Make sure that your Raspberry Pl Pico is connected to your computer and you have selected the MicroPython (Rasaberry Pi Pico) interpreter. Look at the Shell panel at the bottom of the Thonny editor. You should see something like this: Thonny is now able to communicate with your Raspberry Pi Pico using the REPL (read-oval-print loop], wich allows you to type Python code into the Shell and sce the output. Now you can type commands directly into the Shell and they will run on your Raspberry Pi Pico, Type the following command, Tap the Enter key and you will see the output: hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint wnat 218923, 07:24 Geting started with Raspberry Pi Pleo MicroPython adds hardware-specific modules, such as machine, that you can use to prograrn your Raspberry Pi Pic. Let's create a machine. Pin object to correspond with the onboard LED, which can be accessed using GP!O pin 25. Ifyou set the value of the LED to 1, it turns on. Enter the following code, make sure you tap Enter after each line. fron maening pons in ed = Pin(25, Pina You should see the onboard LED light up. Type the code to set the value to 0 to turn the LED off. Turn the LED on and off as many times as you like. Tip: You can use the up arrow on the keyboard to quickly access previous lines. Ifyou want to write a longer program, then it best to save it ina fle. You'll do this in the next step. hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint sam 218923, 07:24 Geting started with Raspberry Pi Pleo Step 6 Blink the onboard LED The Shell is useful to make sure everything is working and try out quick commands. However, it's better to put longer programs in a file ‘Thonny can save and run MicroPython programs directly on your Raspberry Pi Pico. In this step, you will create a MicroPython program to blink the onboard LED on and off in a loop. Click in the main editor pane of Thonny, Enter the following code to toggle the LED. ted. toes) Click the Run button to run your code Thonny will ask whether you want to save the file on This computer or the MicroPython device. Choose MicroPython device. Th Whereto save to? x This computer MicroPython device Enter blink. py as the fle name. Tip: You need to enter the . py fle extension so that Thonny recognises the file as a Python fie. Thonny can save your program to your Raspberry Pi Pico and run it. You should see the onboard LED switch between an and off each time you click the Run button hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint s321 23123, 07:24 Geting started with Raspberry Pi Pleo You can use the Timer module to seta timer that runs a function at regular intervals. Update your code so it looks lke this: rom machine inpors Pin, Timer ea ~ Pin(25, pin-out) ‘ner = Toner) er bline(ttner) tec toggle) ‘ner init (¢reg-2-5, moce-Tiner PERIODIC, cal2backbLink) Click Run and your program will blink the LED on and off until you click the Stop button. hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint 218923, 07:24 Geting started with Raspberry Pi Pleo Step7 Use digital inputs and outputs Now you know the basics, you can learn to control an external LED with your Raspberry Pi Pico, and get it to read input from a button, Use a resistor between about 50 and 380 ohms, an LED, and a pair of M-M jumper leads to connect up your Raspberty Pi Pico as shown in the image below. In this example, the LED is connected to pin 15. f you use a different pin, remember to look up the numberin the pinout diagram in the Meet Raspberry Pi Pico section (‘htm Use the same code as you did te blink the onboard LED, but change the pin number to 15. eg = Pins, Pin oF ‘inorsnse(¢reg-2.5, aace-Tiner PERIODIC, cabacksblink) Run your program and your LED should start to blink. Ifi’s not working, check your wiring to be sure that the LED is connected, Next, let's try and control the LED using a button. hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint 521 23123, 07:24 Geting started with Raspberry Pi Pleo ‘Add a button to your circuit as shown in the .gram below. v ‘The button is on pin 14, and is connected to the 3.3V pin on your Raspberry Pi Pico, This means when you set up the pin, you need to tell MicroPython that itis an Input pin and needs to be pulled down. Create a new fle and ad¢ this code, v rom machine inpor* Pin moore tine ed = Pin(as, pin cut) button = Pin(36, Pln.1N, Pin Pun pom) shite true 1 batton.valve) ed toggle’) ‘ine. s1699(0.8) Run your code and then when you press the button, the LED should toggle on or off f you hold the button > down, it will fash. hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint 23123, 07:24 Geting started with Raspberry Pi Pleo Step 8 Control LED brightness with PWM. Pulse width modulation ('htips://en.wikipedia.org/wiki/Pulse-width, modulation), allows you to give analogue behaviours to digital devices, such as LEDs, This means that rather than an LED being simply on or of, you can control its brightness. For this activity, you can use the circuit fram the last step. Open a new fle in Thonny and add the following code v “rom machine Inport Pin, Pa ron tine inport es = Pa pan(35)) pe freg(2000) shite true “for dty $0 range( $5825) pum euty_ut6cesty) ‘1eep(0, 8081) for duty in range(6se25, 8, -1) amity (ty) ‘eep(6- 9061) Save and run the file. You should see the LED pulse bright and dim, in a continuous cycle. v ‘The frequency (pum. freq) tells Raspberry Pi Pico how often to switch the power between on and off for the LED. ‘The duty cycle tells the LED for how long it should be on each time. For Raspberry Pi Pico in MicroPython, this can range from 0 to 65025. 65025 would be 100% of the time, so the LED would stay bright. A value of around 32512 ‘would indicate that it should be on for half the time. Have apiay withthe pin. freq() values anc the pm. duty 16 value, as well as the length of time for ‘the sleep, to get a feel for how you can adjust the brightness and pace of the pulsing LED. hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint sre 218923, 07:24 Geting started with Raspberry Pi Pleo Step 9 Control an LED with an analogue input Your Raspberry Pi Pico has input pins that can receive analogue signals. This means that instead of only reading the values of 1 and 0 (on and off), it can read values in between, A potentiometer is the perfect analogue device for this activity, Replace the button in your circuit with a potentiometer. Follow the wiring diagram below to connect it to an analogue pin. Ina new fie in Thonny, you can first read the resistance of the potentiometer. ‘Add this code to a new file, and then run it. ‘ine. sloep(3 Turn the potentiometer to sae your maximum and minimum values. They should be approximately between 0 and 65025, hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint v0 23123, 07:24 Geting started with Raspberry Pi Pleo You can now use this value to control the duty eycle for PWM on the LED. Change the code to the following. Once you have run it, tune the dial on the potentiometer to control the LED's brightness, ‘rom machine Inport Pin, Pat, ADC = Pat P3n(18)) fae = A0C(PAN(26)) wn fr0a(s008) shite true ‘uty » ade. nese at6() em. duty_st6(cuty) hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint 23123, 07:24 Geting started with Raspberry Pi Pleo ‘Step 10 Power your Raspberry Pi Pico I you want to run your Raspberry Pi Pico without it being attached to @ computer, you need to use a USB power supply. Safe operating voltages are between 7.8V and 5.5V. ‘To automatically run a MicroPython program, simply save it to the device with the name main. py In Monny, lek onthe Fle menu anathen Save as forthe ast program you wrte, y When prompted, select ‘MicroPython device’ from the pop-up menu. v Name your file main. py v You can now cisconnect your Raspbery Pi Pico fom your computer and use a moro USB cable to connect |_| it to a mobile power source, such as a battery pack. Once connected, the main.py fle should run automatically so you can interact with the components attached to your Raspberry Pi Pico. hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint 20124 218923, 07:24 Geting started with Raspberry Pi Pleo Step 11 What next? + Try the new Introduction to Raspberry Pi Pico path (https://projects.raspberrypl.org/en/pathways/pl ‘), using the picozero package, to control LEDs and buzzers and read signals from switches and dials. ‘+ why not try out a few more components with your Raspberry Pi Pico ~ perhaps a buzzer. alight-dependent resistor (LDR), or even a motor controller ‘+ For further guidance on using your Raspberry Pi Pico, you can have a look at the documentation here (https://w 1rg/documentation/pico/getting-started/) ww.raspberrys Did you enjoy the project? Have you spotted a mistake? Please click the Send feedback button below and let us know! Published by Raspberry Pi Foundation (https://www.raspberrypi.org) under a Creative Commons license (htt ps://creativecommons.org/licenses/by-sa/4.0/). View project & license on GitHub (https://github.com/RaspberryPiLearning/getting-started: 2) th hntpsprojects.raspbenyp.orgenvprojectsgeting-started-uith-the-peoiprint awa

You might also like