If you are new to Arduino IDE programming, see this guide. If you are familiar with Arduino but not on the Circuit Playground Express, see this guide.
Reading analog pins is built into the Arduino environment, available as the standard function call analogRead
. The simple sketch below reads the value of a potentiometer attached to the pin defined by PIN
. For Circuit Playground Express this is A1
, but it could be any analog pin.
Display to Serial
The following is a simple program that reads a potentiometer connected to A1 and displays that value to the Arduino Serial console. The sample output is shown above.
// SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries // // SPDX-License-Identifier: MIT // Simple read analog potentiometer on Circuit Playground Express or other board with pin change // Anne Barela for Adafruit Industries 9/2018 #define ANALOGPIN A1 // For Circuit Playground Express int delayval = 500; // delay for half a second void setup() { Serial.begin(9600); // open the serial port at 9600 bps } void loop() { int value; value = analogRead(ANALOGPIN); // analog read of potentiometer Serial.println(value); // print value delay(delayval); // Delay for a period of time (in milliseconds). }
Display on NeoPixels
As in previous examples, using a potentiometer on a microcontroller, either the Circuit Playground Express or another board allows use of NeoPixels. External pixels are fine. The code below uses the onboard pixels on a Circuit Playground Express to display the value.
// SPDX-FileCopyrightText: 2018 Anne Barela for Adafruit Industries // // SPDX-License-Identifier: MIT // Read analog potentiometer on Circuit Playground Express or other board with changes // Anne Barela for Adafruit Industries 9/2018 based on // NeoPixel Ring simple sketch (c) 2013 Shae Erisson // released under the GPLv3 license to match the rest of the AdaFruit NeoPixel library #include <Adafruit_NeoPixel.h> #ifdef __AVR__ #include <avr/power.h> #endif // Which pin on the microcontroller board is connected to the NeoPixels? #define PIN 8 // For Circuit Playground Express // How many NeoPixels are attached to the board? #define NUMPIXELS 10 // When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals. // Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest // example for more information on possible values. Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); int delayval = 500; // delay for half a second void setup() { Serial.begin(9600); pixels.begin(); // This initializes the NeoPixel library. } void loop() { int i; // loop variable int value; // analog read of potentiometer int display_value; // number of NeoPixels to display out of NUMPIXELS // Read PIN value and scale from 0 to NUMPIXELS -1 value = analogRead(A1); Serial.print(value); Serial.print(", "); display_value = int(value * NUMPIXELS / 1023); Serial.println(display_value); // For a set of NeoPixels the first NeoPixel is 0, second is 1, all the way up to the count of pixels minus one for(i=0; i<display_value; i++){ // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255 pixels.setPixelColor(i, 0, 050, 0); // Moderately bright green color } for(i=display_value; i<NUMPIXELS; i++) { pixels.setPixelColor(i, 0, 0, 0); // turn off all pixels after value displayed } pixels.show(); // This sends the updated pixel color to the hardware. delay(delayval); // Delay for a period of time (in milliseconds). }
Circuit Playground Express NeoPixels are on digital pin 8. The analogRead reads the potentiometer on pin A1.
Page last edited January 22, 2025
Text editor powered by tinymce.