Reading a PWM value with an Arduino from a NodeMCU

Hello everybody !

I am using an arduino to handle too much thing for a NodeMCU.

But still, I need wifi =p

I am trying to use a Segmented switch, with 5 cases and want each on of this case doing something different to my arduino.

I could use a digitalWrite for each case of my NodeMCU, but the thing is, I already use D1 D2, so the only left pins are D5 D6 D7 (dont need boot issues if other pins re pulled low/high).

So I am good to an analogWrite(pin,1);analogWrite(pin,2); and so, for my differents cases.

BLYNK_WRITE(V1) {
  switch (param.asInt())
  {
    case 1: { // Item 1
        nbjoueurs = 2 ;
        break;
      }
    case 2: { // Item 2
        Serial.println("3");
        nbjoueurs = 3 ;
        break;
      }
    case 3: { // Item 3
        Serial.println("4");
        nbjoueurs = 4 ;
        break;
      }
    case 4: { // Item 4
       Serial.println("5");
       nbjoueurs = 5 ;
        break;
      }
    case 5: { // Item 5
        Serial.println("6");
       nbjoueurs = 6 ;
        break;
      }
    }
}

Then in my loop :

if (nbjoueurs == 2)
{
analogWrite(pinnb, 2);
Serial.println("2");
}
else if (nbjoueurs == 3)
{
analogWrite(pinnb, 3);
Serial.println("3");
} 
else if (nbjoueurs == 4)
{
analogWrite(pinnb, 4);
Serial.println("4");
} 
else if (nbjoueurs == 5)
{
analogWrite(pinnb, 5);
Serial.println("5");
} 
else if (nbjoueurs == 6)
{
analogWrite(pinnb, 6);
Serial.println("6");
} 

With pinnb set to #define pinnb 12 //

With my arduino, I am reading this pin with A0

int nbesp = A0;

and in my loop :

nb = analogRead(nbesp);  // read the input pin
Serial.println(nb);  

But when serialprinting to check if its ok when I push the segmented switch, all I got with the Mega is radnom value from 664 to 667, not 2, 3, 4, 5, 6 …

What I am doing wrong ? =p

Thanks a lot !

PWM is not putting out a specific number. That is analogWrite(pinnb, 6); is not going to output the number 6. try to research what PWM is and what it is actually outputting.

Plus I think there is also going to be a voltage difference. ESP PWM 0-3.3V, Arduino Analog 0-5V.

Can I still deal with the PWM if I find a way or a calculation to actually properly “read” it ?

I would think so. Use the serial monitor to read the incoming values of various PWM settings then code to use those values. You may need to use a range as I am not sure how stable the PWM outputs would be, I suspect there may be some “noise” that could cause the values to fluctuate.

Why are you doing this in your void loop?

Pete.

True ! I have juste changed it.

But still, I cant do want I would like … =p

I’d suggest that you post four full amended code, along with details of your serial output.

Pete.

Thanks pete.

I mean, I am trying to do as Toro suggest :

But all I got, whatever PWM value and case I am pushing is value between 664 and 670 …

to test on Node MCU:

analogWrite(0);
delay(500);
analogWrite(100);
delay(500);
analogWrite(200);
delay(500);
analogWrite(300);
delay(500);
analogWrite(400);
delay(500);
analogWrite(500);
delay(500);
analogWrite(600);
delay(500);
analogWrite(700);
delay(500);
analogWrite(800);
delay(500);
analogWrite(900);
delay(500);
analogWrite(1023);
delay(500);

then on the Arduino connected to the serial monitor:

nb = analogRead(A1);  // read the input pin
Serial.println(nb);  

See what values you get for the different analogWrite commands, and them use those values to

I thought that the Mega only used 512 PWM levels?

Pete.

I’ll keep you in touch as soon as I can go further into this project.

If its too complicated to deal with the PWM, I think I am going to use all of the others Pin.

I already use D1 and D2, but I need to be able to change two parameters with wifi. The first one has 5 values and the second one 2 values.

So I am going to go almost full pins with param1 and D3-4-5-6-7 and param2 and SDD2-3 and a simple digital reading from 7 differents arduino pins.