-RESOLVED- ATtiny85 battery voltmeter question

Hey everyone! I am working on a project which is a 1 cell lipo battery alarm that uses an ATtiny85. I want it to beep whenever the voltage of the battery is below the threshold of 3.5V. As well, I want to be able to stop it beeping by pushing a button. I currently have the circuit built, and have the voltage sensing part of the code and alarm part of the code working ok. However, I can’t get it to stop beeping when the button is pressed. What I thought would work is whenever the button is read as High (there is a pull down resistor on the button pin), I would just make an infinite and empty loop. But the device just keeps beeping when I push the button down.

I tried to comment the code as best I could

int buzzerPin = 0; //active buzzer on pin 0
int buttonPin = 3; //button connected to vcc and to pin 3. Pin 3 also has a pulldown resistor
int batteryPin = A1; //analog input pin the battery voltage is applied to 
float batteryVoltage = 0.0; 
float batteryAlertThreshhold = 3.5; //the threshold at which the buzzer will sound
int stopBuzz = LOW;



void setup() {
  pinMode(buzzerPin, OUTPUT); //set the buzzer pin as output
}

void loop() {
  
  batteryVoltage= (analogRead(batteryPin)) * (5.0 / 1023.0); //the math to calculate voltage from an analog read
  stopBuzz = digitalRead(buttonPin); //reads the button to see if the buzzing should stop

  if (stopBuzz==HIGH){ //if the button was pushed
      while(true); //run this empty loop
  }
  
  if ((batteryVoltage <= batteryAlertThreshhold) && (stopBuzz == LOW)){ // if the battery is below the threshold and the button wasnt pressed, then beep
    digitalWrite(buzzerPin, HIGH); 
  }
  else{  //dont sound the buzzer
    digitalWrite(buzzerPin, LOW);
  }

}

Any help is greatly appreciated!
Thanks, Eric

Do you want to measure a LiPo battery with the ATtiny85 connected to a stable/regulated 5volt supply.

Or do you want to measure the battery with the ATtiny powered from the same battery.

Your code will work for the first setup, but not for the second setup. As your battery voltage goes down, so will (default) Aref. The A/D will always return the same A/D value, independent of the battery voltage.

A solution could be to use the internal 1.1volt bandgap reference. https://www.arduino.cc/en/Reference/AnalogReference

No experience with that MCU, but this also might work. http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/ Leo..

Surely you need to turn the buzzer off first?

 if (stopBuzz==HIGH){ //if the button was pushed
      digitalWrite(buzzerPin, LOW);
      while(true); //run this empty loop
  }

There is a system for measuring the voltage of the battery that is powering the Arduino in this link

...R

Hey everyone, thanks for all of the replies and help, I managed to get it working. Originally I had intended to use a 5V regulator to power the circuit, and just read the voltage on an analog pin, but I decided to try and use the internal 1.1V refrence and vcc instead. http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/ This link was great and I implemented it’s code, and the battery alarm works ok now.

int buzzerPin = 0; //active buzzer on pin 0
int buttonPin = 3; //button connected to vcc and to pin 3. Pin 3 also has a pulldown resistor
int batteryPin = A1; //analog input pin the battery voltage is applied to 
int stopBuzz = LOW;
long batteryAlertThreshhold = 3500;


void setup() {
  pinMode(buzzerPin, OUTPUT); //set the buzzer pin as output
}

void loop() {
  
  
  stopBuzz = digitalRead(buttonPin); //reads the button to see if the buzzing should stop

  if (stopBuzz==HIGH){ //if the button is pushed
      digitalWrite(buzzerPin, LOW);
      while(true); //run this empty loop
  }
  
  if ((getVoltage() <= batteryAlertThreshhold) && (stopBuzz == LOW)){ // if the battery is below the threshold and the button wasnt pressed, then beep
    digitalWrite(buzzerPin, HIGH); 
  }
  else{  //dont sound the buzzer
    digitalWrite(buzzerPin, LOW);
  }

}

long getVoltage() {//this function was taken from: http://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/
  
  ADMUX = _BV(MUX3) | _BV(MUX2);
  
  delay(2); // Wait for Vref to settle
  ADCSRA |= _BV(ADSC); // Start conversion
  while (bit_is_set(ADCSRA,ADSC)); // measuring

  uint8_t low  = ADCL; // must read ADCL first - it then locks ADCH  
  uint8_t high = ADCH; // unlocks both

  long result = (high<<8) | low;

  result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
  
  ADMUX&=~(REFS0);
  ADMUX&=~(REFS1);
  
  return result; // Vcc in millivolts
}

Thanks, Eric