How to know if something is receive in PWM

Hi everyone.

Is there a method that can display if something is connected to my Arduino ?
My project : I'm trying to display the duty cycle, but when i disconnect the PWM sender, it still display the last value in the register ...

Thank you

Welcome to the forum

Please post your current sketch

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 </> icon above the compose window) to make it easier to read and copy for examination

There is no problem in my sketch, I just would like to know if a method exist

I did not say or imply that there was a problem with your sketch but if you post it then we can see what you are currently doing. How, for instance, are you reading the incoming PWM signal ?

Oh sorry, here it is :

#include <EEPROM.h>
#define PWC A5
#define PWM_PININ 5
#define PWM_PINOUT 6
#define _DEBUG_ 0

#define _RESETGLOBALPOWER 1
#define _GETGLOBALPOWER 2
#define _GETINSTANTPOWER 3
#define _CHANGEADDRESS 4
#define _SETCURRENTLIMIT 5
#define _GETCURRENTLIMIT 6

//VAR READING MESSAGE
int idxData = 0;
byte mByte;
byte nbMSG;
byte myAddress;

//VAR SENDING MESSAGE
byte cpA = 0;
byte cpC = 1;
byte cpMB = 5;
byte cpD = 3;
byte ok = 0;

//STATE READING MESSAGE
enum state {ST_MSG0, ST_MSG1, ST_MSG2, ST_MSG3, ST_END, ST_MSGL, ST_MSGH, ST_NEND, ST_ZERO, ST_ONE, ST_TWO, ST_THREE, ST_FOUR};
enum state current_state;
//STRUCT MESSAGE         
struct tmessageM { byte address;
                 byte command;
                 byte data[16]; } ;           
struct tmessageM BMsgIn;

//STATE POWER COUNT
enum state power_state;

//VAR POWER COUNT
unsigned long startCount;
unsigned long stopCount;
unsigned long diffCount;
unsigned long powerCount;
unsigned long counter;
byte dataValid = 0;
byte test;
//PWM VAR


void setup() 
{
  pinMode(PWC, INPUT);
  pinMode(PWM_PININ, INPUT_PULLUP);
  pinMode(PWM_PINOUT, OUTPUT);
  current_state = ST_MSG0;
  power_state = ST_ZERO;
  Serial.begin(19200);
  myAddress = EEPROM.read(1);
  diffCount = 0;
  startCount = millis();
}

void loop() 
{
  PowerCount();
  ReadMessage();
  delay(1);
}

//Reading and decomposition of the 4 bytes received
byte ReadMessage()
{ 
  byte cp; 
  byte incomingByte;
  if (Serial.available() > 0) 
  {
    incomingByte = Serial.read();
    
    switch (current_state)
    {
      case ST_MSG0: 
      cp = (incomingByte >> 5) & 0x03; 
      if ( cp == 0 ) 
      {
        BMsgIn.address = incomingByte & 0x0F;
        if ( myAddress == BMsgIn.address )
        {
          current_state = ST_MSG1;
        }
      }
      else
      {
        current_state = ST_MSG0;
      }
      break;
    
      case ST_MSG1: 
        cp = (incomingByte >> 5) & 0x03; 
        if ( cp == 1 ) 
        {
          BMsgIn.command = incomingByte & 0x1f; 
          current_state = ST_MSG2;
        }
        else 
        {
          current_state = ST_MSG0;
        }
      break;
      
      case ST_MSG2: 
        cp = (incomingByte >> 5) & 0x03; 
        mByte = (incomingByte >> 4) & 0x01;  
        if ( cp == 2 ) 
        {
          if (mByte == 0)
          {
            BMsgIn.data[0] = incomingByte & 0x0F;
            current_state = ST_MSG3;
          }
          else
          {
            nbMSG = incomingByte & 0x0F;
            idxData = 0;
            current_state = ST_MSGL;
          }
        }
        else 
        {
          current_state = ST_MSG0;
        }
      break;
      
      case ST_MSG3:
        cp = (incomingByte >> 5) & 0x03; 
        if ( cp == 3 ) 
        {
          BMsgIn.data[0] = BMsgIn.data[0] + ((incomingByte & 0x0F) << 4);
          current_state = ST_END; 
          goto LB_END;
        }
        else 
        {
          current_state = ST_MSG0;
          break;
        }
      
      case ST_MSGL:
        // on vérifie le CP
        cp = (incomingByte >> 5) & 0x03; 
        if ( cp == 3 ) 
        {
          if ( ((incomingByte >> 4) & 0x01) == 0 ) // checking if it's DATA L
          {
            // on extrait la donnée L et on la met dans DATA[idxData] = incomingByte & 0x0F);
            BMsgIn.data[idxData] = incomingByte & 0x0F;
            current_state = ST_MSGH;
          }
          else // This is not DATA L
          {
            current_state = ST_MSG0;
          }
        }
        else 
        {
          current_state = ST_MSG0;
        }
        break;
      
      case ST_MSGH:
        // on vérifie le CP

        cp = (incomingByte >> 5) & 0x03; 
        if ( cp == 3 ) 
        {
          if ( ((incomingByte >> 4) & 0x01) == 1 ) // checking if it's DATA H
          {
            BMsgIn.data[idxData] = BMsgIn.data[idxData] + ((incomingByte & 0x0F) <<4);
            idxData++;
            if ( idxData < nbMSG )
            {
              current_state = ST_MSGL;
              break;
            }
            else
            {
              current_state = ST_END;
              // on se laisse aller vers case ST_END:
            } 
          }
          else // This is not DATA H
          {
            current_state = ST_MSG0;
            break;
          }
        }
        else // This is not Cp = 3
        {
          current_state = ST_MSG0;
          break;
        }

LB_END:
      case ST_END:
        DispatchCommandM(&BMsgIn);
        current_state = ST_MSG0;
      break;
    } 
  }
}

//Management of the order received
byte DispatchCommandM(struct tmessageM *msg)
{
  byte ret = -1;
  unsigned long ret2;
  switch (msg->command)
  {
    case _RESETGLOBALPOWER:
      ret = (byte)ResetGlobalPower();
      SendAnswerM(msg,ret);
     break;
    case _GETGLOBALPOWER:
      ret2 = GetGlobalPower();
      SendAnswerM(msg,ret);
      ret = 1;
     break;
    case _GETINSTANTPOWER:
      ret2 = GetInstantPower();
      SendAnswerM(msg,ret2);
      ret = 1;
     break;
    case _SETCURRENTLIMIT:
      ret = SetCurrentLimit(msg->data[0]);
      SendAnswer(msg,ret);
     break;
    case _GETCURRENTLIMIT:
      ret2 = GetCurrentLimit();
      SendAnswerM(msg,ret2);
     break;
    case _CHANGEADDRESS:
      ret = ChangeAddress(msg->data[0]);
      SendAnswer(msg,ret);
     break;
  }  
  return ret;
}

byte SendAnswerM(struct tmessageM *msg, unsigned long retmsg)
{
  byte msgOut[11];
  msgOut[0] = msg->address + (cpA << 5);
  msgOut[1] = msg->command + (cpC << 5); 
  msgOut[2] = (cpMB << 4) + 8; 
  for ( int i = 0; i < 8; i++)
  {
    msgOut[3+i] = (cpD << 5) + ((retmsg >> (4*i)) & 0x0f);
  }
  Serial.write(msgOut, 11);
  return 0;
}

byte SendAnswer(struct tmessageM *msg, byte retmsg)
{
  byte msgOut[11];
  msgOut[0] = msg->address + (cpA << 5);
  msgOut[1] = msg->command + (cpC << 5); 
  msgOut[2] = (cpMB << 4) + 8; 
  for ( int i = 0; i < 8; i++)
  {
    msgOut[3+i] = (cpD << 5) + ((retmsg >> (4*i)) & 0x0f);
  }
  Serial.write(msgOut, 11);
  return 0;
}

byte PowerCount()
{
  byte iCount;
  unsigned long currentTime;
  unsigned long tlimit;

  iCount = digitalRead(PWC);
  tlimit = diffCount*2;
  currentTime = millis();
  if ( (dataValid == 1) && ((currentTime - startCount) > tlimit ) && (iCount == 1) ) 
  {
    dataValid = 0;
    power_state = ST_ONE;
    //Serial.println("BP1");
  }
  switch (power_state)
  {
    case ST_THREE:
    if ( iCount == 0 )
    {
      stopCount = millis();
      dataValid = 1;
      powerCount++;
      diffCount = stopCount - startCount;
      startCount = stopCount;
      power_state = ST_FOUR;
    } 
    break;
    
    case ST_FOUR:
    if ( iCount == 1 )
    {
     power_state = ST_THREE;
    } 
    break;
    
    case ST_ONE:
    if ( iCount == 0 )
    {
      startCount = millis();
      powerCount ++;
      power_state = ST_TWO;
    }
    break;
    
    case ST_TWO:
    if ( iCount == 1 )
    {
     power_state = ST_THREE;
    } 
    break;
  
    case ST_ZERO:
    if ( iCount == 1 ) 
    {
      power_state = ST_ONE;    
    }
    break;
  }
}
byte ResetGlobalPower()
{
  powerCount = 0;
  return 1;
}

unsigned long GetGlobalPower()
{
  return powerCount;
}

unsigned long GetInstantPower()
{
  unsigned long instantPower;
  if ( dataValid == 0 ) 
  {
    return 0;
  }
  else 
  {
    instantPower = 3600000 / diffCount;
    return instantPower;
  }
}

byte SetCurrentLimit(byte PWM) // Valid value 1..99 - Cote chargeur
{
  uint16_t temp = (PWM * 256)/100; 
  analogWrite(PWM_PINOUT, temp);
  return 1;
} 

int GetCurrentLimit()
{
  int pwm = pulseIn(PWM_PININ, HIGH);
  int temp = pwm/10;

  return temp;
}

byte ChangeAddress(byte address)
{
  EEPROM.put(1,address);
  return 1;
}

I'm working on a charge car station, and since I don't have an electric car to test it, I tried to simulate one

A simple timeout?

A simple timeout ? In the pulseIn ?

The input is set to INPUT_PULLUP so if nothing is connected the input will read HIGH. If your PWM never goes to 100% then you can use the 100% to indicate "Nothing Connected".

So if I get " HIGH " when i read my pin, that's mean nothing is connected ?

If you are expecting pulses and are getting a continuous HIGH then it is likely that the thing that produces the pulses is not connected.

Are you trying to read the Control Pilot signal? That's +/- 12V at 1 kHz so you'll need some external circuitry to get that to the 0-5V range. Some steering diodes and voltage dividers should do the trick. Check out the OpenEVSE project.

Yes, I try to read the Control pilot signal.

Have you a schema so I can know how to do it ?

Thank you !

Thank you !

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