Loading...
Searching...
No Matches
SerialPlotter.ino

Streams the encoder, potentiometer and temperature readings to the Arduino IDE Serial Plotter every 200 ms. The encoder switch selects which conditioned temperature channel is read, MCU or EXT.

Jumpers
JP1 at Internal (on-board U1), JP2 at Internal (RP2040 ADC), JP3 at 3.3 V.
Output
Tools → Serial Plotter at 115200 baud, one line per sample:
Label Value
Encoder Encoder count, 2 per click (AnalogShield::EncoderCount())
Potentiometer RV1 wiper voltage, in V
Sensor Raw LMT84 sensor (SENSOR_IN, A1), in °C
Temperature Conditioned signal on the selected channel, in °C
Channel Selected analog input, 0 for MCU (A0), 3 for EXT (A3)

For example:

Encoder:12,Potentiometer:1.650,Sensor:22.41,Temperature:22.37,Channel:0
Channel selection
Each press of the encoder switch changes the channel; the switch is debounced in onSwitch(). The LED for the selected channel flashes, 500 ms on and 500 ms off:
Channel Pin LED Channel
MCU TEMP_SIG_MCU, A0 Green D1 0
EXT TEMP_SIG_EXT, A3 Red D2 3
Notes
  • JP2 routes the conditioned signal to only one of A0 and A3, so move it to External when selecting EXT; the other channel reads an unconnected pin.
  • Each reading averages SAMPLES ADC reads at 12-bit resolution (10 bits on an Uno R3), which steadies the value.
/*
* SerialPlotter
* Streams the encoder, potentiometer and temperature readings to the Arduino
* IDE Serial Plotter every 200 ms. The encoder switch selects which
* conditioned temperature channel is read, MCU or EXT.
*
* Jumpers: JP1 at Internal (on-board U1), JP2 at Internal (RP2040 ADC), JP3 at 3.3 V.
* Output: Tools → Serial Plotter at 115200 baud, one line per sample:
* Encoder encoder count, 2 per click (AnalogShield::EncoderCount())
* Potentiometer RV1 wiper voltage, in V
* Sensor raw LMT84 sensor (SENSOR_IN, A1), in °C
* Temperature conditioned signal on the selected channel, in °C
* Channel selected analog input, 0 for MCU (A0), 3 for EXT (A3)
* For example
* Encoder:12,Potentiometer:1.650,Sensor:22.41,Temperature:22.37,Channel:0
*
* Each press of the encoder switch changes the channel; the switch is
* debounced in onSwitch(). The LED for the selected channel flashes, 500 ms
* on and 500 ms off:
* MCU (TEMP_SIG_MCU, A0) green D1, Channel = 0
* EXT (TEMP_SIG_EXT, A3) red D2, Channel = 3
* JP2 routes the conditioned signal to only one of A0 and A3, so move it to
* External when selecting EXT; the other channel reads an unconnected
* pin.
*
* Each reading averages SAMPLES ADC reads at 12-bit resolution (10 bits on an
* Uno R3), which steadies the value.
*/
#include <AnalogShield.h>
using namespace AnalogShieldPins;
static AnalogShield Shield;
constexpr uint8_t SAMPLES = 16;
constexpr uint32_t PRINT_MS = 200; // time between plotter lines
constexpr uint32_t FLASH_MS = 500; // LED on and off time
constexpr uint32_t DEBOUNCE_MS = 50; // presses closer together than this are switch bounce
// Changed by the switch callback, so volatile
volatile bool use_ext = false;
// Runs on every press and release: keep it short, no Serial or delay()
void onSwitch(bool pressed) {
static uint32_t last_press = 0;
if (!pressed) {
return;
}
const uint32_t now = millis();
if (now - last_press >= DEBOUNCE_MS) {
use_ext = !use_ext;
}
last_press = now;
}
void setup() {
Serial.begin(115200);
Shield.begin();
Shield.SetAdcResolution(12);
Shield.SetSwitchCallback(onSwitch);
}
void loop() {
static uint32_t last_flash = 0;
static uint32_t last_print = 0;
static bool led_on = false;
const uint32_t now = millis();
const bool ext = use_ext;
// Flash the LED for the selected channel; keep the other one off
if (now - last_flash >= FLASH_MS) {
last_flash = now;
led_on = !led_on;
}
Shield.LedON(LED1, led_on && !ext);
Shield.LedON(LED2, led_on && ext);
if (now - last_print >= PRINT_MS) {
last_print = now;
const TempSource channel = ext ? TempSource::EXT : TempSource::MCU;
// label:value pairs, comma separated, one line per sample
Serial.print("Encoder:");
Serial.print(Shield.EncoderCount());
Serial.print(",Potentiometer:");
Serial.print(static_cast<float>(Shield.PotMillivolts(SAMPLES)) / 1000.f, 3);
Serial.print(",Sensor:");
Serial.print(Shield.TempCelsius(TempSource::SENSOR, SAMPLES), 2);
Serial.print(",Temperature:");
Serial.print(Shield.TempCelsius(channel, SAMPLES), 2);
Serial.print(",Channel:");
Serial.println(ext ? 3 : 0); // analog input number, A3 or A0
}
}
Library for the U of T Physics Analog Shield (Introduction to Electronics).
TempSource
Temperature signal to read with AnalogShield::TempRaw() or AnalogShield::TempMillivolts().
Definition AnalogShield.h:52
@ MCU
Conditioned signal on TEMP_SIG_MCU (A0, RP2040 ADC), JP2 at Internal.
Definition AnalogShield.h:54
@ SENSOR
Raw sensor output on SENSOR_IN (A1).
Definition AnalogShield.h:53
@ EXT
Conditioned signal on TEMP_SIG_EXT (A3, MAX11635 external ADC on the Phyduino Pico),...
Definition AnalogShield.h:55
Encoder, switch, temperature signals, potentiometer and LEDs on the Analog Shield.
Definition AnalogShield.h:64
void LedON(uint8_t index, bool on=true) noexcept
Turns an LED on or off.
Definition AnalogShield.cpp:266
float TempCelsius(TempSource source, uint8_t samples=1) const
Reads a temperature signal and converts it to degrees Celsius.
Definition AnalogShield.cpp:240
bool SetAdcResolution(uint8_t bits)
Sets the ADC resolution used by analogRead() and by every reading below.
Definition AnalogShield.cpp:171
int32_t EncoderCount() const noexcept
Encoder steps since begin() or the last SetEncoderCount().
Definition AnalogShield.cpp:93
int32_t PotMillivolts(uint8_t samples=1) const
Reads the potentiometer wiper voltage.
Definition AnalogShield.cpp:256
void begin()
Sets pin modes, turns both LEDs off and starts tracking the encoder and its switch.
Definition AnalogShield.cpp:56
void SetSwitchCallback(SwitchCallback callback) noexcept
Sets the function called when the encoder switch is pressed or released.
Definition AnalogShield.cpp:146
Shield signals by Arduino pin, from the Rev-C schematic.
Definition AnalogShield.h:26
constexpr uint8_t LED1
D1 green LED, active low (anode to VDD).
Definition AnalogShield.h:44
constexpr uint8_t LED2
D2 red LED, active low (anode to VDD).
Definition AnalogShield.h:43