PWM usage for EV

Im using an Arduino Nano Every to measure a PWM signal from a BMS module.
However im not getting the right values.

My multimeter is measuring 7.8KHz and a PWM of 36.8%.

This is my result.

Is there anything wrong with the code?
Do i need to put a resistor somewhere in the circuit? To ground?

Any help is greatly appreciated.

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

For one thing, the integer match around line 46 may do funny things and certainly never produce anything with decimals.

Maybe start by posting a clear schematic of your setup and a good photo that shows how everything is connected. Then upload the full code of your sketch as text, not as an image.

Finally, be aware of the possibilities and limitations of 'pulseIn'. I think you may want to look at the use of timers and the input capture functionality they offer.


```cpp
#include <Wire.h>

const int PWM_input = 3;
int PWM;

void setup() {
  pinMode(PWM_input, INPUT);

  Wire.begin(8);
  //Wire.onReceive(receiveEvent);
  //Wire.onRequest(requestEvent);

  Serial.begin(9600);
}

void loop() {
  GetPWM();
  Serial.println(PWM);
  delay(100);
}
/*
void receiveEvent(int howMany) {
  while (1 < Wire.available()) { // loop through all but the last
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);         // print the character
  }
  int x = Wire.read();    // receive byte as an integer
  Serial.println(x);         // print the integer
}

void requestEvent(){
  Wire.write("Hello");
}
*/
void GetPWM() {
  long highTime = pulseIn(PWM_input, HIGH);
  long lowTime = pulseIn(PWM_input, LOW);

  if (highTime == 0) {
    PWM = 0;
  } else if (lowTime == 0) {
    PWM = 100;
  } else {
    PWM = ((100 * highTime) / (highTime + lowTime));
  }
  Serial.println(highTime);
  Serial.println(lowTime);
  Serial.println();
}

The PWM signal from the BMS is connected directly to P3 from the Nano Every.

Is it a 5V signal?
and GND

Along with common GND?

Definitely a good idea. Calling pulseIn() twice does not capture from the same PWM cycle which introduces jitter. The Nano Every has a timer mode that is able to do so.

See 21.3.3.1.6 Input Capture Frequency and Pulse-Width Measurement Mode in the CPU data sheet.

Moreover, there will be unanticipated behavior depending on when the function is called; if you do this at some point halfway during a PWM cycle, you'll just get bogus data. It really only works if you start counting immediately after the rising/falling slope - in which case a Timer-based approach or perhaps even something using a pin change interrupt will work just as well, or even better.

It is a 5V signal and has a common ground.
How would I do that interrupt instead of the pulse in?

Take a look at attachInterrupt

What does the Arduino print out?
Your screen shot is blurry

The high pulse and low pulse are both around 9900 and the PWM is 50%

Do you have a circuit diagram and some photos?

Does it help to set the pinMode to INPUT_PULLUP?

That is a little suspicious. I have a feeling that the signal is not getting to pin D3 and pulseIn maybe timing out

When I set the input as PULLUP all the values go to 0.

That confirms my suspicions.

After measuring the PWM signal with a multimeter at the Nano every also had some bad signals. Turns out the plug wasn't making a good connection. Now my measurements are the same on the Nano every as well as on the multimeter.

Case closed!
Have fun!

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