Arduino to esp32 PWM signal problem

Hi everyone, I need to use Arduino as PWM signai generator for esp32 controller. I need to generate 3.3V Volt signal on Arduino and send it to esp32 pin to read and perform an action.

For some reason it's not working at all. esp32 receives values 0-400 when when arduino board button is not pushed (sounds like a noise to me), and I don't see 4000+ values on serial when Arduino button is pushed.

Here's an Arduino Code:


const int buttonPin = 2;         // the number of the pushbutton pin
const int LEDpin = 13;           // the number of the LED pin
const int PWMpin = 3;            // the number of the PWM pin
const int PWMVoltage = 168;      // For 3.3V
const int PWMimpulseTime = 500;  // 500ms


// variables will change:
int buttonState = 0;  // variable for reading the pushbutton status

void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  // initialize the LED pin as an output:
  pinMode(LEDpin, OUTPUT);

  // initialize the PWM pin as an output:
  pinMode(PWMpin, OUTPUT);

  Serial.begin(115200);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    Serial.println("high");
    // turn LED on:
    digitalWrite(LEDpin, HIGH);  // turn the LED on (HIGH is the voltage level)
    analogWrite(PWMpin, PWMVoltage);
    delay(PWMimpulseTime);
    analogWrite(PWMpin, 0);
    digitalWrite(LEDpin, LOW);
    Serial.println("low");
  } else {
    Serial.println("low");
  }
}

Here's an esp32 code:

const int PWMpin = 25; 
const int pwmPinStatus;

void setup() {
   pinMode(PWMpin, INPUT);
}

void loop() {
  pwmPinStatus = analogRead(PWMpin);

   if (pwmPinStatus !=0) {
     Serial.print("pwmPinStatus: ");
    Serial.println(pwmPinStatus);
    delay(100);
  }

Wiring diagram, UNFROTUNATELLY I have zero electric schema drawing skill so sharing, Fritzing exported file.

Fritzing File source: FileShareSite.com - Free Online File Hosting

Are you using a pull-down resistor? If not, the pin picks up noise from the environment and reacts on that.

You can wire the button between pin and GND and use pinMode(buttonPin, INPUT_PULLUP);. The logic will be inverted so a pressed button will read LOW.

Arduino UNO is a 5V device and EP32 is a 3.3V device. Use a voltage divider to reduce the voltage level for the safe operation of ESP32. You may use the following divider circuit which will also serve DPin-3 of UNO from being floated.


Figure-1:

You are missing data type for the destination variable in the baove code.

Hi.

You are missing data type for the destination variable in the baove code.

Thanks alot, I've added value type for a code.

Arduino UNO is a 5V device and EP32 is a 3.3V device. Use a voltage divider to reduce the voltage level for the safe operation of ESP32. You may use the following divider circuit which will also serve DPin-3 of UNO from being floated.

But I thought that this code will actually send something around 3.3 Volts, so there's no need for voltage division. But for some reason I see no PWM levels > 400, while expected PWM value to receive on esp32 analog pin is 4000+, when I'm pushing a button. Am'I wrong in that assumption?

...
const int PWMVoltage = 168;      // For 3.3V
const int PWMimpulseTime = 500;  // 500ms
...
    analogWrite(PWMpin, PWMVoltage);
    delay(PWMimpulseTime);
...

Hi,
The weird thing that I think I'm using it, button push is registered as it should be, and I see this both in Serial console and by LED which light up on button push. So the only thing puzzles me is PWM behavior.

Thanks again for your answer, I've added Fritzing source file, if possible could you edit it to reflect what you mean ? Here is the file: FileShareSite.com - Free Online File Hosting

I would like to recommend you to perform the following experiment; where, you may find answers to some your questions which are not clear from your post.

1. Build the followig hardware setup of Fig-1. In this setup, you slowly turn the Pot, the duty cycle of PWM signal will change whch will be filtered by C2 to assume DC volatge of 0V at 0% duty cycle and 3.3V at 100% duty cycle. The voltage will be received by ESP32 and will be displayed on its Serial Monitor.


Figure-1:

2. Upload the following sketches:

UNO Sketch:

void setup()
{
  Serial.begin(9600);
}

void loop() 
{
  unsigned int y = analogRead(A0);
  byte dutyCycle = map(y, 0, 1024, 0, 255);
  analogWrite(3, dutyCycle);
  delay(1000);
}

ESP32 Sketch:

void setup()
{
  Serial.begin(9600);
}

void loop() 
{
  unsigned int y = analogRead(25);
  float testVolt = (3.3/4095)*y;
  Serial.println(testVolt, 1);  //received volt 1-digit after decimal point
  delay(1000);
}

3. Slowly turn Pot of UNO.

4. Check that Serial Monitor of ESP32 shows changing volages.
OUTPUT on ESP32

9:23:31.484 -> 1.2
19:23:32.488 -> 1.2
19:23:33.493 -> 0.9
19:23:34.482 -> 0.9
19:23:35.483 -> 0.8
19:23:36.483 -> 0.6
19:23:37.505 -> 0.7
19:23:38.509 -> 1.3
19:23:39.512 -> 1.7
19:23:40.514 -> 2.3
19:23:41.516 -> 2.8
19:23:42.522 -> 2.9
19:23:43.475 -> 3.3

Why do you need Arduino to generate PWM?
It can generate PWM from ESP.

I want to test compatibility with quite expensive tool which I don't plan to buy. This tool has integration pins which generate 3.3V PWM signal, so I took Arduino I had at hand to do just that - generate a signal, on which my controller has to react.

Hello, thanks alot. I've ordered missing components, like capacitors and pots. I'll definitely try that.

1 Like

Thanks alot for this schema, it solved my problem. I'm able to send PWM signal and it actually triggers an action. I would close this topic as resolved, if I'd knew how.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.