Reading Analog Input / Output with GLYPH

This guide will help you read Analog input using a Potentiometer and write it to an LED to control it's brightness, assuming you are using GLYPH-C3 (but any GLYPH development board)
A potentiometer is a variable resistor with three terminals that adjusts voltage by sliding a wiper across a resistive track. It works as a voltage divider(A voltage divider is a simple circuit that reduces voltage using two resistors in series. It’s commonly used to step down voltages for sensors, ADC inputs, or interfacing components with different voltage levels), where the output voltage varies based on the wiper's position.
When you move the wiper of a potentiometer (variable resistor), it changes the resistance between the wiper and the two end terminals, which in turn varies the output voltage according to the voltage divider rule.
A potentiometer works as a variable voltage divider with three terminals:
-
Two outer terminals connect to a fixed resistance.
-
The middle terminal (wiper) moves along the resistive track, changing the resistance ratio.
-
As the wiper moves, the output voltage varies proportionally between the input voltage and ground.
When a potentiometer is connected to a Glyph board, it acts as a variable voltage divider, generating an analog voltage that controls the LED brightness using PWM (Pulse Width Modulation) or DAC (Digital-to-Analog Converter).
This is achieved through 2 techniques:
-
Analog to Digital Conversion (ADC) - This is a peripheral on the GLYPH board which is used to convert Analog signals like Voltage to Digital values. This allows GLYPH to read and process the analog signals. The Resolution on the GLYPH series boards is 12-bit (0 - 4095)
-
Pulse Width Modulation (PWM) - This peripheral on the GLYPH is used to output an Analog-like signal from digital signal. This is done by modifying the Pulse-Width/Duty cycle of the digital signals generated by the GLYPH. Alternatively you can use DAC on the GLYPH but it would be complicated for beginners.
Step 1: Hardware Required
-
GLYPH-C3 Board
-
LED
-
220 Ohm Resistor
-
Potentiometer - 47k
-
Breadboard
-
Male to Male Jumper Wires
Step 2: Circuit Diagram
Connect the circuit as per the following Circuit Diagram:

Step 3: Code Setup
In this program, the Configuring and Writing of LED PWM Value can be done in 2 Ways:
- Method 1 - Using ledcWrite() function for ESP32 based GLYPH Boards
- Method 2 - Using analogWrite() function for Arduino Compatible GLYPH Boards
(Both functions are included in the latest ESP32 Documentation here- ESP32 LED Library Functions)
- Open Arduino IDE
- Enter the following code into the Arduino IDE
- Method 1 : Using ledcWrite() function
// Pin number of the LED
const int led = 1; // which is Digital pin D0 in GLYPH-C3
// Pin number of the Potentiometer
const int pot = 2; //, which is Analog pin A1 in GLYPH-C3
// Setting PWM Properties like Frequency and Resolution
const int freq = 5000; // LEDC PWM channel’s Frequency Range is 10 Hz - 40 MHz.So, set a Frequency value in this range
const int resolution = 8; // The Default Resolution for LEDC PWM is 8 bits
void setup()
{
// Initialize the Serial Monitor Baud Rate as 115200 (you can also set it to 9600)
Serial.begin(115200);
pinMode(led, OUTPUT); // Set LED to be an output pin
pinMode(pot, INPUT); // Set Potentiometer to be an input pin
// Setting up the LEDC pin with the PWM Properties and Attaching this pin to ADC Channel 0, where the Potentiometer is connected
ledcAttachChannel(led,freq,resolution,0);
}
void loop()
{
// Read the Analog input voltage between 0 V and 3.3 V from the Potentiometer and map it into corresponding integer values between 0 and 4095 using C3's 12-bit ADC8
int potvalue = analogRead(pot);
// Changing the Potentiometer's ADC range from 0 - 4095 to the LED PWM duty cycle range 0 - 255 (As we are using 8-bit resolution for PWM) using the equation (255.0/4095.0)*potvalue
int brightness = (255.0/4095.0)*potvalue;
ledcWrite(led, brightness); // Changing the LED brightness by setting the above PWM Duty cycle
// Print the LED brightness value onto Serial Monitor
Serial.print("Led Brightness is:");
Serial.println(brightness);
}
- Method 2 : Using analogWrite() function
// Pin number of the LED
const int led = A1; // which is Digital pin A1 in GLYPH-C3
// Pin number of the Potentiometer
const int pot = A0; //, which is Analog pin A1 in GLYPH-C3
void setup()
{
// Initialize the Serial Monitor Baud Rate as 9600 (you can also set it to 115200 if you need more number of serial printings per second)
Serial.begin(9600);
pinMode(led, OUTPUT); // Set LED to be an output pin
pinMode(pot, INPUT); // Set Potentiometer to be an input pin
}
void loop()
{
// Read the Analog input voltage between 0 V and 3.3 V from the Potentiometer and map it into corresponding integer values between 0 and 4095 using the 12-bit ADC of Glyph Boards
int potvalue = analogRead(pot);
// Converting the Potentiometer's ADC range from 0 - 4095 to the LED PWM duty cycle range 0 - 255 using the equation (255.0/4095.0)*potvalue
int brightness = (255.0/4095.0)*potvalue;
analogWrite(led, brightness); // Set the LED brightness using PWM with the calculated duty cycle
// Print the LED brightness value onto Serial Monitor
Serial.print("Led Brightness is:");
Serial.println(brightness);
}
If you are using Method 2, it is not mandatory to set the PWM Resolution and Frequency, as the function analogWrite() by default, generates an 8-bit PWM by setting the Duty cycle values in the range 0 - 255 (255 is the maximum possible duty cycle value that analogWrite() can accept). However, if you wish to set a custom PWM frequency and resolution on a selected pin, you can do so by using the functions analogWriteResolution() and analogWriteFrequency().
The syntax for the analogWriteResolution function in Arduino IDE is :
void analogWriteResolution(uint8_t pin, uint32_t resolution bits)
The syntax for the analogWriteFrequency function in Arduino IDE is :
void analogWriteFrequency(uint8_t pin, uint32_t freq).
The freq parameter is the new PWM frequency in hertz (Hz) units, and the range is 0 to (2^32 - 1) Hz.
Both analogWriteResolution and analogWriteFrequency functions must be called before analogWrite() to apply these settings.
Step 4: Upload the Code
- Connect the Board
- Connect your GLYPH board to your computer
-
Select the Board and Port
Do the following settings in your Arduino IDE, Do the following settings in your Arduino IDE,
Tools > Board > esp32 > Pcbcupid GLYPH C3
For the Pcbcupid GLYPH C3 to appear under Tools > Board > esp32, the esp32 board version installed in the Arduino IDE should be greater than or equal to 3.1.0.
Tools > Portand select the port connected to your GLYPH.Tools > USB CDC on Boot >Enabled
If USB CDC on BOOT not enabled, you won't be seeing any serial data on Arduino IDE.
- Upload the Code
- Click the upload button (➡️ icon) or use the shortcut
CTRL + Uin Arduino IDE to upload the code to the board.
Step 5: Observe the Output
Depending on the Initial Position of the Potentiometer Knob, few cases are possible:
- Case 1 - The LED is in OFF state
In this Case, the Serial Monitor should display the Led Brightness as 0(LOW):


To Turn ON the LED and increase its brightness, slowly turn the Potentiometer Knob in the Clockwise direction. At the Maximum Possible Limit of the Clockwise Rotation, the LED will glow the Brightest.The Serial Monitor should now display the Brightness value which is the Maximum PWM Duty Cycle value as 255(HIGH) or a value in the range 200-255 due to the knob-turning limitations of the potentiometer that we use.
- Case 2 - The LED is in ON state
In this Case, the Serial Monitor should display the Maximum LED Brightness value of 255. Sometimes, It may show a value slightly less than the Max. Brightness value 255(like 216 in our case) due to the knob-turning limitations of the potentiometer that we use.


To Turn OFF the LED and decrease its brightness, slowly turn the Potentiometer knob in the Anti-Clockwise direction. At the Maximum Possible Limit of the Anti-Clockwise Rotation, the LED will not glow at all and its brightness value will again become 0.
- Case 3 - The LED is in ON state with a Brightness value of 22(or numbers close to 22, in the range of 20-30, as an exact value of 22 may be difficult to get by manually turning your potentiometer knob).
The Serial Monitor should now display the value of 22 or values in the range of 20-30 like this:


- Case 4 - The LED is in ON state with a Brightness value of 170(or numbers close to 170) The Serial Monitor should now display the value of 170 or values close to 170:

