Skip to content

Examples

Oren Sokoler edited this page Jan 26, 2024 · 5 revisions

Here are some examples of using the low level Raspberry Pi Pico. I believe the examples are self explanatory. You can also find the source code and build files here. For more information you can look up the Javadoc documentation in the doc directory of the release tar ball.

General Purpose Input/Output (GPIO)

import pico.hardware.GPIOPin;

class Main {

    /* A simple GPIO input test */
    private static final int PIN_GPIO_TEST = 2;

    public static void main(String[] args) {
        System.out.println("GPIO example");
        GPIOPin p = new GPIOPin(PIN_GPIO_TEST, GPIOPin.IN, GPIOPin.PULLUP);
        while (true) {
            int value = p.get();
            if (value == GPIOPin.HIGH) {
                System.out.println("Pin state is HIGH");
            }
            else {
                System.out.println("Pin state is LOW");
            }
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
    
}

Analog to Digital Conversion (ADC)

import pico.hardware.ADCChannel;

class Main {

    /* A simple ADC test */
    private static final int ADC_CHANNEL = 2;

    public static void main(String[] args) {
        System.out.println("ADC example");
        ADCChannel a = new ADCChannel(ADC_CHANNEL);
        while (true) {
            int value = a.read();
            System.out.println("ADC Channel " + ADC_CHANNEL + " value is " + value);
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }
    
}
Clone this wiki locally