ARM Embedded Tutorial - Raspberry Pi Pico using the ADC: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: ARM Embedded Tutorial - Raspberry Pi Pico using the ADC
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ### Introduction | |
| 2 | + | ||
| 3 | + | There are three ADC Inputs available on the Raspberry Pi which can be accessed on | |
| 4 | + | ||
| 5 | + | - ADC0 Pin 31 (GP26) | |
| 6 | + | - ADC1 Pin 32 (GP27) | |
| 7 | + | - ADC2 Pin 34 (GP28) | |
| 8 | + | ||
| 9 | + | the input voltage range of the ADC pins is 0V .. 3.3V | |
| 10 | + | ||
| 11 | + | In addition there is another on-board ADC Input which is connected to a temperature sensor on the chip. | |
| 12 | + | ||
| 13 | + | ### Load the demo project into lazarus | |
| 14 | + | ||
| 15 | + | This example shows you how to read out the analog pins and the internal sensor. | |
| 16 | + | ||
| 17 | + | Please open adc/adc-raspi_pico.lpi in lazarus. | |
| 18 | + | ||
| 19 | + | The example on this page is a stripped down version of the full source code that you see in Lazarus. | |
| 20 | + | ||
| 21 | + | ``` pascal | |
| 22 | + | program adc; | |
| 23 | + | {$MODE OBJFPC} | |
| 24 | + | {$H+} | |
| 25 | + | {$MEMORY 10000,10000} | |
| 26 | + | ||
| 27 | + | uses | |
| 28 | + | pico_gpio_c, | |
| 29 | + | pico_adc_c, | |
| 30 | + | pico_timer_c, | |
| 31 | + | pico_c; | |
| 32 | + | ||
| 33 | + | var | |
| 34 | + | milliVolts,milliCelsius : longWord; | |
| 35 | + | ||
| 36 | + | begin | |
| 37 | + | adc_init; | |
| 38 | + | // Make sure GPIO is high-impedance, no pullups etc | |
| 39 | + | adc_gpio_init(TPicoPin.ADC0); | |
| 40 | + | ||
| 41 | + | // Turn on the Temperature sensor | |
| 42 | + | adc_set_temp_sensor_enabled(true); | |
| 43 | + | ||
| 44 | + | repeat | |
| 45 | + | // Select ADC input 0 (GPIO26) | |
| 46 | + | adc_select_input(0); | |
| 47 | + | ||
| 48 | + | // For now we avoid floating point, there have been fixes done on FPC trunk which are yet to be verified | |
| 49 | + | milliVolts := (adc_read * 3300) div 4096; | |
| 50 | + | ||
| 51 | + | // Select internal temperature sensor | |
| 52 | + | adc_select_input(4); | |
| 53 | + | milliVolts := (adc_read * 3300) div 4096; | |
| 54 | + | ||
| 55 | + | //Temperature formula is : T = 27 - (ADC_voltage - 0.706)/0.001721 | |
| 56 | + | milliCelsius := 27000-(milliVolts-706)*581; | |
| 57 | + | ||
| 58 | + | busy_wait_us_32(500000); | |
| 59 | + | until 1=0; | |
| 60 | + | end. | |
| 61 | + | ``` | |
| 62 | + | ||
| 63 | + | When programming the ADC there is more or less only one thing to be aware of, the Pins are configured with their GPIO Values (26,27,28) but when selecting a ADC Input we need to reference the ADC Inputs by their Name | |
| 64 | + | ||
| 65 | + | - ADC0 --\> 0 | |
| 66 | + | - ADC1 --\> 1 | |
| 67 | + | - ADC2 --\> 2 | |
| 68 | + | - ADC4 --\> 4 | |
| 69 | + | ||
| 70 | + | no big deal, but easy to get wrong..... | |
| 71 | + | ||
| 72 | + | ## See also | |
| 73 | + | ||
| 74 | + | - [[ARM Embedded Tutorial - FPC and the Raspberry Pi Pico|ARM Embedded Raspberry Pi Pico Tutorials]] | |
| 75 | + | ||
| 76 | + | --- | |
| 77 | + | *From the Free Pascal wiki, page "ARM Embedded Tutorial - Raspberry Pi Pico using the ADC", as it stood on 2021-12-30 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 78 | + | Category: ARM | |
| 79 | + | Category: Embedded | |
| 80 | + | Category: Microcontrollers | |
| 81 | + | Category: Operating Systems and Platforms | |
| 82 | + | Category: Tutorials |
