Problem Interfacing PMOS as switch in Automatic Charging and Discharging Project

Hi! Can you please help me with my code? This is my first time using arduino.
The project is about Automatic Charging and Discharging of a Battery using arduino mega2560.I used PMOS as switch for charging and servo motor for pushing the button of the battery to start discharging. I also have sensor LED to know if the battery is empty or in fully charged condition.

I am not sure with my code and I am facing problem with the PMOS. Once the battery is in fully charged condition, the PMOS switch off and the servo motor will move that makes battery to discharge.But after 5sec, the PMOS will switch on again and the battery will stop discharging.

I hope that someone will help me regarding this project.
Thank you in advance.

I attached files here.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

const int analogInPin = A0;  // Analog input pin that the photosensor is attached to

int sensorValue = 0;      

void setup() 

{

  myservo.attach(9);  // attaches the servo on pin 9 to the servo object

  pinMode(13, OUTPUT); // initialize digital pin 13 as an output.
   
  Serial.begin(9600); // initialize serial communications at 9600 bps:


}

void loop() {   // read the analog in value:

sensorValue = analogRead(analogInPin); // change the analog out value:


// print the results to the Serial Monitor:
  
  Serial.print("sensor = ");

  Serial.print(sensorValue);

  Serial.print("\n");
  
  if (sensorValue >=555)
  {
    delay(100);
    
    digitalWrite(13, LOW); // turn the LED off by making the voltage LOW

    delay(5000); // wait for a second
  
    myservo.write(90);

     delay(5000);

    myservo.write(0);

     delay(5000);
     
    Serial.print("Battery Full: Discharge ON");

    Serial.print("\n");
      
  }

 else if( sensorValue = 5 )

  {

    digitalWrite(13, HIGH); // turn the LED On by making the voltage high

    delay(5000); // wait for a second    

  }  

  delay(1000) ;

}

good luck

else if( sensorValue = 5 )

Should be ==

Thank you very much Grumpy_Mike for the reply.
I tried to change the else if condition , from else if( sensorValue = 5 ) into else if( sensorValue == 5 ) .
The PMOS continuously switching Off that makes the battery continuously discharging.But once the battery reached its empty condition, the Pmos could not be able to switch on and the battery could not charge.And also, the servo motor continuously moving as the Sensor LED reach the max resolution value which is 557. The servo motor should move only once. Kindly help me what to do.

Thank you so much.

In this situation you should never use an equals test always a less that test.

I think you need to post a schematic of your wiring and post your corrected code and say what it does and what you would like it it do. These points are not clear from your post above.

Hello.

Thank you very much for the reply last month about my problem in my arduino project.Now I will send again some information for better undertanding of my project/
As I told before the project is about Automatic Charging and Discharging of a Battery using arduino mega2560.I used PMOS as switch for charging and servo motor for pushing the button of the battery to start discharging. I also have LED Sensor circuit to know if the battery is in empty or in fully-charged condition.

As you can see in the vide, once maximum sensor value which is 565 will display in serial monitor,it means that the battery is now in fully-charged condition, the PMOS switch off and the servo motor will move in order to push the ON button of battery and start to discharge.But after 5sec, the PMOS will switch on again and the battery will stop discharging.

Thank you very much and well appreciated to your help.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0;  // Analog input pin that the photosensor is attached to
int sensorValue = 0;      

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(13, OUTPUT); // initialize digital pin 13 as an output.
   
   // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {

   // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // change the analog out value:

   // print the results to the Serial Monitor:
  Serial.print("sensor = ");
  Serial.print(sensorValue);
  Serial.print("\n");
  
  if (sensorValue >=565 )
  {
    delay(10);
    
    digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
    delay(5000); // wait for a second
  
    myservo.write(90);
     delay(5000);

    myservo.write(0);
     delay(5000);
     
    Serial.print("Battery Full: Discharge ON");
    Serial.print("\n");
      
  }
 else if( sensorValue ==0  )

  {
    delay(100);
    digitalWrite(13, HIGH ); // turn the LED On by making the voltage high
    delay(5000); // wait for a second    
  }  
  delay(5000) ;
}

For Consultation.pdf (1.21 MB)

delay(5000); // wait for a second

Actually waits for 5 seconds.

That LED sensor circuit looks wrong, what does your printout say the sensor value is when it turns off?

But after 5sec, the PMOS will switch on again and the battery will stop discharging.

That 5 seconds is only because you have a five seconds delay at the end of the loop. So it looks like your sensor is not working properly.

Why use that circuit anyway? Why not just connect each sensor to a digital pin directly and enable the pull up resistors and do a digital read on each of those sensors to discover which LEDs are on?

Printout says the sensor value is 0 when it turns off.

I thought that I need circuit that will sense the LED of the battery that's why I made LED Sensor circuit.

Do I made a lot of mistake? I am very sorry.

Can you please send me image for the correct connection of my project as well as the code?

Please help me.

Thank you very much.

Printout says the sensor value is 0 when it turns off.

So that means the code is doing what it is supposed to do, but your sensor is not detecting a discharge state, or your battery is broken.

I thought that I need circuit that will sense the LED of the battery that's why I made LED Sensor circuit.

That is reasonable but where did you get the sensor circuit from? It seems it is not working correctly. Does it return a value of zero with some if the LEDs on?

Your problem seems to be with this sensor circuit and not your code. Have you tested just the sensor circuit to see if it follows the batteries LEDs? Is it being interfered with by ambient light?

Can you please send me image for the correct connection of my project as well as the code?

We don’t do things for you on this forum, we help you get things right

I have just tested the LED sensor circuit only with the arduino. LED of the battery are all off and the display in serial monitor was 0,0,0,0,0,1,20,31,0,0,0. It seems that the LED sensor Circuit is still sensing light not from the led of the battery but from ambient light. So I tried to cover the led of the LED sensor circuit to make sure that it will never sense ambient light and the display from the serial monitor was all 0.

So does your code work properly now?

If I will test LED sensor circuit only with the arduino, I think it works properly but if I will test the whole system including the PMOS circuit and servo it seems that it is not working properly that's why I thought there is something wrong with the code.

Before you said

LED of the battery are all off and the display in serial monitor was 0,0,0,0,0,1,20,31,0,0,0.

Is this still the case? Or do you now get zeros but the code still turns on.

Sorry to keep on asking but you have the system not me. What I am asking is what I would do if I had the system. The code looks OK but I could have missed something, that is why using serial print to see what is happening, and what value variables are is a vital tool in fault finding.

I think there is no problem with the LED sensor circuit. I made changes with my if else condition and it works. But sometimes, there is still problem when the battery is near to its fully charge condition. And I think there is still problem with my if else condition on my code that I have changed.I attached one file showing 2 different serial monitor displays. The servo should move at 90 deg once the display on SERIAL MONITOR No. 2(see attached file) will appear. Can you please teach me what is the problem with my if else condition?

Thanks a lot.

Here is my new if else condition:

if (sensorValue1 >=569 && sensorValue2 >=521 && sensorValue3 >=418 && sensorValue4 >=250 && state==0 )
    {  
      state=1;
      delay(5000);
      digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
      delay(5000); // wait for a second
    
       myservo.attach (9);
       myservo.write(30);
       delay(3000);
       myservo.detach ();
       delay (1000);
       
       myservo.attach (9);
       myservo.write(0);
       delay(3000);
       myservo.detach ();
       delay (1000);
     
      Disctr = Disctr + 1;
        
       delay(900);
       
      Serial.print("Battery Full: Discharge ON");
      Serial.print("\n");
      
    
   }
  else if((sensorValue1 <=96 && sensorValue2 <=95 && sensorValue3 <=97 && sensorValue4 <=96) &&
         (sensorValue1 >=1 && sensorValue2 >=1 && sensorValue3 >=1 && sensorValue4 >=1))
     { 
      state=0;
      delay(5000);
      digitalWrite(13, HIGH ); // turn the LED On by making the voltage high
      delay(5000); // wait for a second    
      
       Chactr = Chactr + 1;
      delay(900);

      Serial.print("Battery Empty: Charging ON");
      Serial.print("\n");

Serial Monitor Display.pdf (381 KB)

Grumpy_Mike:
Before you saidI s this still the case? Or do you now get zeros but the code still turns on.

The trick in getting a good solution is to answer the questions asked of you.

At the moment we have no schematic apart from two identical circuits posted with the opening post. It would help enormously if we could see what you have for your detector circuit and how this connected to your Arduino. Also regarding the circuit you posted then it would help to use the proper symbols. I know this is a combined transistor and resistor in a package but the best way to show that is to draw the circuit of it in the data sheet, the one with the conventional transistor symbol and then surround this with a dotted box and label the pin numbers on the perimeter of the box.

You could be suffering from a interference or you might need some hysteresis in your code but without a schematic and you answering your questions I am flying blind.

Thank you very much for the help.

I will check first the schematic again and try to revise it.