Wiki copymirror

This is a mirror of the wiki at pascal.chat. Editing, history and sign-in are there.

ARM Embedded Tutorial - Raspberry Pi Pico using the ADC

Introduction

There are three ADC Inputs available on the Raspberry Pi which can be accessed on

the input voltage range of the ADC pins is 0V .. 3.3V

In addition there is another on-board ADC Input which is connected to a temperature sensor on the chip.

Load the demo project into lazarus

This example shows you how to read out the analog pins and the internal sensor.

Please open adc/adc-raspi_pico.lpi in lazarus.

The example on this page is a stripped down version of the full source code that you see in Lazarus.

pascal
program adc;
{$MODE OBJFPC}
{$H+}
{$MEMORY 10000,10000}

uses
  pico_gpio_c,
  pico_adc_c,
  pico_timer_c,
  pico_c;

var
  milliVolts,milliCelsius : longWord;

begin
  adc_init;
  // Make sure GPIO is high-impedance, no pullups etc
  adc_gpio_init(TPicoPin.ADC0);

  // Turn on the Temperature sensor
  adc_set_temp_sensor_enabled(true);

  repeat
    // Select ADC input 0 (GPIO26)
    adc_select_input(0);

    // For now we avoid floating point, there have been fixes done on FPC trunk which are yet to be verified
    milliVolts := (adc_read * 3300) div 4096;

    // Select internal temperature sensor
    adc_select_input(4);
    milliVolts := (adc_read * 3300) div 4096;

    //Temperature formula is : T = 27 - (ADC_voltage - 0.706)/0.001721
    milliCelsius := 27000-(milliVolts-706)*581;

    busy_wait_us_32(500000);
  until 1=0;
end.

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

no big deal, but easy to get wrong.....

See also


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.

Revision 1, last changed by WikiArchive .