element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Pi-Fest
  • Challenges & Projects
  • Design Challenges
  • Pi-Fest
  • More
  • Cancel
Pi-Fest
Blog Playing with Raspberry Pi Pico #2
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Leaderboard
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: emaker
  • Date Created: 6 Aug 2022 6:05 PM Date Created
  • Views 2155 views
  • Likes 6 likes
  • Comments 2 comments
  • Pi-Fest Design Challenge
  • thonny
  • raspberry pi pico
  • micropython
  • pifest
Related
Recommended

Playing with Raspberry Pi Pico #2

emaker
emaker
6 Aug 2022

Hi, as I said in the first blog, the purpose of this work is to acquire the knowledge of Raspberry Pi Pico board and to start using it for simple applications. I will use these blogs to introduce my students to the use of this board and its programming. Obviously, let's not forget the theme of this contest, so we will also talk about sound.

Introduction

The sound is caused by pressure waves that propagate through the air, something very different from the “1” and “0” sequences that digital devices use. As in the case of other physical quantities, Nature uses analog quantities while our devices use digital quantities
The Raspberry Pi Pico has 4 inputs that can be used to interface with analog signals, such as electrical signals received from an analog microphone. The user can only use 3 since the fourth is reserved for use with the internal temperature sensor of the board and will therefore be used by the Raspberry itself.

image


Looking at the pinout scheme, already reported in the first blog, we will see that the 3 pins that we can use for the conversion are:

  1. ADC0 pin31 GP26
  2. ADC1 pin32 GP27
  3. ADC2 pin34 GP28

These 3 pins are the inputs of 3 analog-to-digital converters (ADCs). The ADCs of the Raspberry Pi Pico are 12-bit and therefore allow you to quantize the input signal using 4096 different levels (212 = 4096), a rather interesting quality for simple IoT applications.
The analog-to-digital conversion operation is quite complex and, in the board manual, we can see that the time required to acquire a sample is about 2µs, which corresponds to a sample-rate of 500 kS/s.
In this blog I would like to play a bit with the analog-to-digital converter, taking care of connecting the board and writing the code to use.


Let's try the Raspberry Pi Pico ADC

The simplest way to test an ADC is to use a potentiometer. Connecting the two lateral terminals respectively to GND and VCC, by turning the knob of the potentiometer, we will obtain on the cursor all the voltages between 0V (GND) and, in our case, 3.3V (VCC).
Potentiometer wiring diagram between pin 33 AGND and pin 36 3V3 (OUT), cursor on pin 31 AD0.

image

image

The code

As for the code, it is very trivial and could also be managed directly by Putty but, for convenience, we used another equally light, portable and effective software tool, namely the Thonny IDE.
It is a minimal IDE and does not have the advanced features of other software tools but, for a student, it is a fantastic tool due to its ease of use.


From the “Tools / Options” menu, going to the “interpreter” tab, you can configure the IoT board we are using and the COM port to which it is connected.

image

Let's see the code.

I import the modules I need:
    import machine
    import utime

I configure the ADC0 input:
    analog_value = machine.ADC (26)

I read the voltage value on pin GP26 and display it:
    reading = analog_value.read_u16 ()
    print ("ADC:", reading)

Summarizing the code will be:

import machine
import utime
analog_value = machine.ADC (26)
reading = analog_value.read_u16 ()
print ("ADC0:", reading)

image


You can repeat the voltage reading on the potentiometer cursor indefinitely and then obtain the voltage values ​​after a certain time, for simplicity I set the time at 0.2 seconds, setting a sleep time of 0.2 seconds
The code becomes:

import machine
import utime
analog_value = machine.ADC (26)
while 1:
    reading = analog_value.read_u16 ()
    print ("ADC0:", reading)
    utime.sleep_ms (200) #wait 0.2 seconds

image

We expect that, by turning the potentiometer from one extreme to the other, it will go from a minimum value of "0" to a maximum of "4096", since we are using a 12-bit ADC. In theory, therefore, since we will use two bytes to hold the captured binary value, we will only work in the range of values ​​from 0 to 4096 and the values ​​from 4097 to 16K will be unused.
In practice, however, by turning the potentiometer, you can see that all 16 bits are used and the printed values ​​will go from 0 to 64K. This obviously is not a "miracle" but it is one of the features of the MicroPython that uses the two bytes entirely, obviously there will always be only 4096 different levels.


Coming soon we will see how to use a microphone with our Raspberry Pi Pico.
See you next blog!

  • Sign in to reply
Parents
  • DAB
    DAB over 3 years ago

    If the board can read the ADC every 2 microseconds, does it have a DMA capability to store the data into memory at that rate?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jc2048
    jc2048 over 3 years ago in reply to DAB

    datasheets.raspberrypi.com/.../rp2040-datasheet.pdf

    From page 562 of the RP2020 datasheet:

    4.9.2.5. DMA
    The RP2040 DMA (Section 2.5) can fetch ADC samples from the sample FIFO, by performing a normal memory-mapped read on the FIFO register, paced by the ADC_DREQ system data request signal. The following must be considered:

    etc, etc.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • jc2048
    jc2048 over 3 years ago in reply to DAB

    datasheets.raspberrypi.com/.../rp2040-datasheet.pdf

    From page 562 of the RP2020 datasheet:

    4.9.2.5. DMA
    The RP2040 DMA (Section 2.5) can fetch ADC samples from the sample FIFO, by performing a normal memory-mapped read on the FIFO register, paced by the ADC_DREQ system data request signal. The following must be considered:

    etc, etc.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube