Skip to main content

Reading Analog Input / Output with GLYPH

pcbcupid_glyph_intro-led_changebrightnesswithPot 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:
  1. Two outer terminals connect to a fixed resistance.
  2. The middle terminal (wiper) moves along the resistive track, changing the resistance ratio.
  3. 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

  1. GLYPH-C3 Board
  2. LED
  3. 220 Ohm Resistor
  4. Potentiometer - 47k
  5. Breadboard
  6. Male to Male Jumper Wires

Step 2: Circuit Diagram

Connect the circuit as per the following Circuit Diagram: pcbcupid_analog_input

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)
  1. Open Arduino IDE
  2. Enter the following code into the Arduino IDE
  • Method 1 : Using ledcWrite() function
  • Method 2 : Using analogWrite() function
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

  1. Connect the Board
  • Connect your GLYPH board to your computer
  1. 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 > Port and 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.
  1. Upload the Code
  • Click the upload button (➡️ icon) or use the shortcut CTRL + U in 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): pcbcupid_glyphled_pot_minbrightserialmon pcbcupid_glyphled_pot_minbright 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. pcbcupid_glyphled_pot_maxbrightserialmon pcbcupid_glyphled_pot_maxbright 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: pcbcupid_glyphled_pot_22brightserialmon pcbcupid_glyphled_pot_22bright
  • 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:
pcbcupid_glyphled_pot_170brightserialmon pcbcupid_glyphled_pot_170bright