Community project

Encoder Signal Generator

Photo of Encoder Signal Generator
Generated with AI

Teddy's Escapades (Bazmaniac)

Published November 27, 2026

This project uses an ESP32 and TI UCC27531D gate drivers to generate precise quadrature encoder signals for testing and simulation. The dual-channel driver circuit produces clean, protected digital outputs that mimic real encoder behavior, with configurable frequency, pulse-per-revolution counts, and phase relationships.

Builders will receive a complete wiring diagram showing driver connections, component placement, and signal routing, along with a parts list and Arduino firmware that runs on the ESP32. The guide includes step-by-step assembly instructions and testing procedures to verify signal integrity before connecting to machinery or control systems.

Wiring diagram

Wiring diagram for Encoder Signal Generator
Loading diagram…

Gather all the parts

QtyComponent
1

TI UCC27531D (SOIC-8)

Non-inverting 3.3 V logic to 10–30 V push-pull driver. SOIC single output version; pins 1,4,5,8 are NC. TI datasheet SLUSBA7G.

1

Resistor

10 kohm 0.25 W

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

1 kohm 2 W flameproof

Through-hole resistor (current-limiting in series with an LED)

1

Ceramic Capacitor

100 nF 50 V ceramic

Through-hole ceramic capacitor bought by value, such as 22 pF, 10 nF or 100 nF (0.1 µF). It is non-polarised, so either lead can go to either net. Its most common job is decoupling: a 100 nF capacitor between an IC's supply pin and ground, placed as close to the chip as possible.

1

Ceramic Capacitor

1 uF 50 V X7R ceramic

Through-hole ceramic capacitor bought by value, such as 22 pF, 10 nF or 100 nF (0.1 µF). It is non-polarised, so either lead can go to either net. Its most common job is decoupling: a 100 nF capacitor between an IC's supply pin and ground, placed as close to the chip as possible.

1

TI UCC27531D (SOIC-8)

Non-inverting 3.3 V logic to 10–30 V push-pull driver. SOIC single output version; pins 1,4,5,8 are NC. TI datasheet SLUSBA7G.

1

Resistor

10 kohm 0.25 W

Through-hole resistor (current-limiting in series with an LED)

1

Resistor

1 kohm 2 W flameproof

Through-hole resistor (current-limiting in series with an LED)

1

Ceramic Capacitor

100 nF 50 V ceramic

Through-hole ceramic capacitor bought by value, such as 22 pF, 10 nF or 100 nF (0.1 µF). It is non-polarised, so either lead can go to either net. Its most common job is decoupling: a 100 nF capacitor between an IC's supply pin and ground, placed as close to the chip as possible.

1

Ceramic Capacitor

1 uF 50 V X7R ceramic

Through-hole ceramic capacitor bought by value, such as 22 pF, 10 nF or 100 nF (0.1 µF). It is non-polarised, so either lead can go to either net. Its most common job is decoupling: a 100 nF capacitor between an IC's supply pin and ground, placed as close to the chip as possible.

1

Electrolytic Capacitor

10 uF 50 V

Radial aluminium electrolytic capacitor bought by value and voltage rating, such as 10 µF, 100 µF or 1000 µF 10 V. It is polarised: the + lead goes to the higher voltage and the - lead, marked by the stripe on the sleeve, to the lower voltage or ground. It is mostly used for bulk filtering across a power rail, with a voltage rating comfortably above the rail.

Assemble it in 5 steps

1. Mount the two drivers

Use two UCC27531D SOIC-8 chips on a soldered PCB or SOIC adapter boards. Viewed from above, pin 1 is marked by the dot; pins count counterclockwise. Pin 2 is IN, pin 3 is GND, pin 6 is VDD and pin 7 is OUT. Leave pins 1, 4, 5 and 8 unconnected. Do not use the SOT-23 package with this drawing.

  • The SOIC has a single output. The SOT-23 version has different pins and split outputs.

2. Connect the Tough control signals

Port A yellow GPIO32 goes to driver A pin 2 (signal A); white GPIO33 goes to driver B pin 2 (signal B); black goes to both pin 3 grounds. Leave the red 5 V wire insulated. Fit one 10 kohm resistor from each pin 2 to ground to keep its input low during startup.

  • Never connect 10–30 V to the Tough pins. This circuit shares grounds and is not isolated.

3. Wire the adjustable supply and capacitors

Connect regulated supply positive, adjustable from 10 to 30 V, to both driver pin 6 terminals. Connect supply negative to driver grounds, Tough black wire and receiver signal ground. Fit 100 nF and 1 uF ceramic capacitors between pins 6 and 3 at each chip with very short connections. Fit the 10 uF capacitor across the supply nearby, positive to VDD and striped negative to ground. Power Tough by USB; no switching 5 V regulator is needed in this version.

  • Supply overshoot must stay below 32 V. Reversed supply or electrolytic polarity can destroy parts.

4. Connect protected signal outputs

Connect each driver pin 7 through its own 1 kohm 2 W flameproof resistor to receiver A or B. These resistors limit a short to ground to about 30 mA at 30 V, but are not full industrial output protection. Use only high-impedance receiver inputs (recommended at least 100 kohm), not low-resistance loads or differential line inputs.

  • At 100 kohm input resistance, the series resistor causes approximately 1 percent voltage loss. Check the receiver's high/low thresholds and cable capacitance.
  • Do not connect to other driven outputs or external pull-ups. Resistors may become hot during faults. Long industrial cables require additional engineered surge protection.

5. Test before connecting machinery

With power off check all connections. Plug Tough into USB and apply the adjustable driver supply. Measure A and B with a scope rated for 30 V. Both start low. Select Frequency or Conveyor, tap fields, and press Start. A leads B by 90 degrees. Editing or changing modes stops the outputs. Test at 10 V and 30 V with the receiver load before machine use.

  • Diameter is in metres: use 0.1 for a 100 mm wheel. PPR is cycles per revolution per channel, not four-edge counts. Target range is 0.1–1000 Hz; zero conveyor speed keeps outputs stopped.
  • This interrupt-timed generator is not safety-rated. Verify jitter and edges under actual load; do not attach a 3.3 V logic analyser directly to high-voltage outputs.

Review all connections

1. Connections between "driver_a" and "ESP32"

Functiondriver_aESP32
digitalINGPIO 32
groundGNDGND
powerVDD30V
digitalOUT → Resistor P1EXT

2. Connections between "limit_a" and "ESP32"

Functionlimit_aESP32
digitalP2 → Receiver A input (high impedance)EXT

3. Connections between "pull_a" and "ESP32"

Functionpull_aESP32
digitalP1 → TI UCC27531D (SOIC-8) INEXT
groundP2GND

4. Connections between "bypass_a" and "ESP32"

Functionbypass_aESP32
digital1 → TI UCC27531D (SOIC-8) VDDEXT
ground2GND

5. Connections between "local_a" and "ESP32"

Functionlocal_aESP32
digital1 → TI UCC27531D (SOIC-8) VDDEXT
ground2GND

6. Connections between "driver_b" and "ESP32"

Functiondriver_bESP32
digitalINGPIO 33
groundGNDGND
powerVDD30V
digitalOUT → Resistor P1EXT

7. Connections between "limit_b" and "ESP32"

Functionlimit_bESP32
digitalP2 → Receiver B input (high impedance)EXT

8. Connections between "pull_b" and "ESP32"

Functionpull_bESP32
digitalP1 → TI UCC27531D (SOIC-8) INEXT
groundP2GND

9. Connections between "bypass_b" and "ESP32"

Functionbypass_bESP32
digital1 → TI UCC27531D (SOIC-8) VDDEXT
ground2GND

10. Connections between "local_b" and "ESP32"

Functionlocal_bESP32
digital1 → TI UCC27531D (SOIC-8) VDDEXT
ground2GND

11. Connections between "bulk" and "ESP32"

FunctionbulkESP32
power+30V
ground-GND
power+ → Adjustable regulated supply positive (10–30 V)EXT
ground- → Adjustable supply negative and receiver signal groundEXT

Deploy the firmware

#include <Arduino.h>
#include <M5Unified.h>
#include <driver/mcpwm_prelude.h>
#include <cmath>
// Hardware up-count PWM: explicit quarter-cycle edges on both channels.
// No per-edge interrupts, including at 200 kHz.
constexpr int PIN_A=32, PIN_B=33;
mcpwm_timer_handle_t waveTimer=nullptr;
mcpwm_oper_handle_t oper=nullptr, operB=nullptr;
mcpwm_cmpr_handle_t compare=nullptr, compareBHigh=nullptr, compareBLow=nullptr;
mcpwm_gen_handle_t genA=nullptr,genB=nullptr;
bool conveyor=false, running=false, editing=false, aLeads=true;
int field=0;
double hz=100, ppr=1024, diameter=0.1, speed=10, actual=0;
String entry, message;
double requested(){return conveyor ? ppr*speed/(60.0*PI*diameter):hz;}
void stop(){
  if(waveTimer){
    mcpwm_generator_set_force_level(genA,0,true);
    mcpwm_generator_set_force_level(genB,0,true);
    mcpwm_timer_start_stop(waveTimer,MCPWM_TIMER_STOP_EMPTY);
    // Wait for stop at zero before disabling and deleting resources.
    delay((uint32_t)ceil(1000.0/actual)+2);
    mcpwm_timer_disable(waveTimer);
    mcpwm_del_generator(genA);mcpwm_del_generator(genB);
    mcpwm_del_comparator(compare);
    mcpwm_del_comparator(compareBHigh);mcpwm_del_comparator(compareBLow);
    mcpwm_del_operator(oper);mcpwm_del_operator(operB);
    mcpwm_del_timer(waveTimer);waveTimer=nullptr;
  }
  pinMode(PIN_A,OUTPUT);pinMode(PIN_B,OUTPUT);
  digitalWrite(PIN_A,LOW);digitalWrite(PIN_B,LOW);
  running=false; actual=0;
}
void start(){
  stop(); double f=requested();
  if(conveyor && speed==0){message="Zero speed: outputs stopped";return;}
  if(!std::isfinite(f)||f<0.1||f>200000){message="Range: 0.1 to 200000 Hz";return;}
  // Keep the complete period within the ESP32's 16-bit timer.
  uint32_t resolution=f>=1250 ? 80000000 : f>=16 ? 1000000 : 1000;
  uint32_t period=4*(uint32_t)llround(resolution/(4.0*f));
  actual=resolution/(double)period;
  mcpwm_timer_config_t tc={};tc.group_id=0;tc.clk_src=MCPWM_TIMER_CLK_SRC_DEFAULT;
  tc.resolution_hz=resolution;tc.count_mode=MCPWM_TIMER_COUNT_MODE_UP;tc.period_ticks=period;
  ESP_ERROR_CHECK(mcpwm_new_timer(&tc,&waveTimer));
  mcpwm_operator_config_t oc={};oc.group_id=0;
  ESP_ERROR_CHECK(mcpwm_new_operator(&oc,&oper));
  ESP_ERROR_CHECK(mcpwm_operator_connect_timer(oper,waveTimer));
  ESP_ERROR_CHECK(mcpwm_new_operator(&oc,&operB));
  ESP_ERROR_CHECK(mcpwm_operator_connect_timer(operB,waveTimer));
  mcpwm_comparator_config_t cc={};
  cc.flags.update_cmp_on_tez=true;
  ESP_ERROR_CHECK(mcpwm_new_comparator(oper,&cc,&compare));
  ESP_ERROR_CHECK(mcpwm_new_comparator(operB,&cc,&compareBHigh));
  ESP_ERROR_CHECK(mcpwm_new_comparator(operB,&cc,&compareBLow));
  ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(compare,period/2));
  ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(compareBHigh,period/4));
  ESP_ERROR_CHECK(mcpwm_comparator_set_compare_value(compareBLow,3*period/4));
  mcpwm_generator_config_t gc={};gc.gen_gpio_num=aLeads?PIN_A:PIN_B;
  ESP_ERROR_CHECK(mcpwm_new_generator(oper,&gc,&genA));
  gc.gen_gpio_num=aLeads?PIN_B:PIN_A;
  ESP_ERROR_CHECK(mcpwm_new_generator(operB,&gc,&genB));
  ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(genA,
    MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,MCPWM_TIMER_EVENT_EMPTY,MCPWM_GEN_ACTION_HIGH)));
  ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(genA,
    MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,compare,MCPWM_GEN_ACTION_LOW)));
  ESP_ERROR_CHECK(mcpwm_generator_set_action_on_timer_event(genB,
    MCPWM_GEN_TIMER_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,MCPWM_TIMER_EVENT_EMPTY,MCPWM_GEN_ACTION_LOW)));
  ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(genB,
    MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,compareBHigh,MCPWM_GEN_ACTION_HIGH)));
  ESP_ERROR_CHECK(mcpwm_generator_set_action_on_compare_event(genB,
    MCPWM_GEN_COMPARE_EVENT_ACTION(MCPWM_TIMER_DIRECTION_UP,compareBLow,MCPWM_GEN_ACTION_LOW)));
  ESP_ERROR_CHECK(mcpwm_timer_enable(waveTimer));
  ESP_ERROR_CHECK(mcpwm_timer_start_stop(waveTimer,MCPWM_TIMER_START_NO_STOP));
  running=true; message="";
}
void button(int x,int y,int w,int h,const String& text,uint16_t color=0x3186){
  M5.Display.fillRoundRect(x,y,w,h,4,color);
  M5.Display.setTextColor(TFT_WHITE,color);
  M5.Display.drawCentreString(text,x+w/2,y+6,2);
}
void draw(){
  M5.Display.fillScreen(TFT_BLACK);
  if(editing){
    M5.Display.setTextColor(TFT_WHITE,TFT_BLACK);
    M5.Display.drawString("Enter value: "+entry,8,5,2);
    const char* keys[]={"1","2","3","4","5","6","7","8","9",".","0","Del"};
    for(int i=0;i<12;i++)button((i%3)*106+2,35+(i/3)*39,102,35,keys[i]);
    button(2,195,152,40,"Cancel");button(160,195,158,40,"OK",0x0400);return;
  }
  button(2,2,155,32,"Frequency",!conveyor?0x0400:0x3186);
  button(162,2,156,32,"Conveyor",conveyor?0x0400:0x3186);
  if(conveyor){
    button(5,42,310,30,String("PPR: ")+String(ppr,0));
    button(5,76,310,30,String("D (metres): ")+String(diameter,6));
    button(5,110,310,30,String("V (m/min): ")+String(speed,3));
  }else button(5,55,310,45,String("Frequency Hz: ")+String(hz,3));
  M5.Display.setTextColor(TFT_WHITE,TFT_BLACK);
  M5.Display.drawString(String("Target: ")+String(requested(),3)+" Hz",5,145,2);
  M5.Display.drawString(running?String("RUN: ")+String(actual,3)+" Hz":"STOPPED - A/B low",5,164,2);
  M5.Display.setTextColor(TFT_YELLOW,TFT_BLACK);
  M5.Display.drawString(message,5,183,2);
  button(5,207,150,30,running?"Stop":"Start",running?0xF800:0x0400);
  button(165,207,150,30,aLeads?"A leads B":"B leads A");
}
void edit(int f){stop();field=f;entry="";editing=true;message="";draw();}
void setup(){
  pinMode(PIN_A,OUTPUT);pinMode(PIN_B,OUTPUT);
  digitalWrite(PIN_A,LOW);digitalWrite(PIN_B,LOW);
  auto cfg=M5.config();cfg.fallback_board=m5::board_t::board_M5Tough;
  cfg.internal_spk=false;cfg.internal_mic=false;cfg.internal_imu=false;
  M5.begin(cfg);
  // Use landscape for the 320 x 240 controls and keypad.
  // Select by actual dimensions rather than assuming rotation 1 is landscape.
  M5.Display.setRotation(0);
  if(M5.Display.width()<M5.Display.height()) M5.Display.setRotation(1);
  M5.Display.setBrightness(180);
  draw();
}
void loop(){
  M5.update();auto t=M5.Touch.getDetail();
  if(t.wasPressed()){
    int x=t.x,y=t.y;
    if(editing){
      if(y>=35&&y<191&&x>=0&&x<320){
        int k=((y-35)/39)*3+x/106;
        if(k<9&&entry.length()<12)entry+=String(k+1);
        else if(k==9&&entry.indexOf('.')<0&&entry.length()<12)entry+='.';
        else if(k==10&&entry.length()<12)entry+='0';
        else if(k==11&&entry.length())entry.remove(entry.length()-1);
      }else if(y>=195){
        if(x<160){editing=false;}
        else {double v=entry.toDouble();
          if(entry.length()&&std::isfinite(v)&&((field==3&&v>=0)||(field!=3&&v>0))){
            if(field==0)hz=v;if(field==1)ppr=round(v);if(field==2)diameter=v;if(field==3)speed=v;
            if(ppr<1)ppr=1;editing=false;message="Press Start to apply";
          }
        }
      }
      draw();
    }else if(y<34){stop();conveyor=x>=160;message="";draw();}
    else if(conveyor&&y>=42&&y<140){edit(1+(y-42)/34);}
    else if(!conveyor&&y>=55&&y<100){edit(0);}
    else if(y>=207&&x<160){if(running)stop();else start();draw();}
    else if(y>=207&&x>=165){stop();aLeads=!aLeads;message="Direction changed: press Start";draw();}
  }
  delay(5);
}

Remix this project

Make it yours in one click

Open a full copy of this project in your own Schematik workspace, diagram, code, parts, and assembly steps included. Swap the sensor, add features, or redesign the whole thing with AI. The author's original stays untouched.

Open in Schematik