getting a reading from OLED on attiny85

I have been working on miniatuarizing this battery tester using an attiny85 and OLED 128x32. I can connect this fine using and arduino in which the OLED will read the voltage and an LED will light up green, yellow or red for new, medium, or bad battery. I need help connecting everything in the correct place.

I'm connecting:

the SCL of the OLED to pin 7
the SDA of the OLED to pin 5

green LED to pin 6
yellow LED to pin 2
red LED to pin 3
zener diode to pin 7

I know the actual pins on the attiny are different than on the sketch as pin 0 starts on pin 5 on the attiny,
1 is pin 6, 2 is pin 7, 3 is pin 2 and 4 is pin 3. See the pic I attached.

The code seems to want to work as the OLED will display a voltage and 0.00-0.01 reading will fluctuate and only the red LED will turn on. Am I connecting the OLED, LED and zener diode correctly? I am sharing pin 7 with the zener diode and SCL of the OLED. If I cant share pins, I don't mind taking one LED off.

Here is my code

#include <Tiny4kOLED.h>


int greenLed =  1;  //must be pin 2
int yellowLed = 3; //pin 3
int redLed = 4;    //pin 4

int analogValue = 0;
float voltage = 0;
int ledDelay = 1000;
// ============================================================================

void setup() {
  // Put your own setup code here, to run once.
  oled.begin();
  oled.clear();

  // Turn on the display
  oled.on();

  oled.switchRenderFrame();
pinMode(greenLed, OUTPUT);
  pinMode(yellowLed,OUTPUT);
  pinMode(redLed,OUTPUT);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  
  oled.clear();

  oled.setFont(FONT8X16);
  
  oled.setCursor(32, 0);
  
  oled.print(F("Voltage"));

  oled.setFont(FONT6X8);

  oled.setCursor(13, 2);
 
  oled.print(F(""));
  
  oled.setCursor(16, 3);
  
  oled.print(F(""));
  
  oled.switchFrame();

  delay(3000);
analogValue = analogRead(2);
  voltage = 0.0048*analogValue;
 oled.println(voltage);
  

  if( voltage >= 1.6 )
    digitalWrite(greenLed, HIGH);
  else if (voltage > 1.2 && voltage < 1.6)
    digitalWrite(yellowLed, HIGH);
  else if( voltage <= 1.2)
    digitalWrite(redLed, HIGH);  

  delay(ledDelay);
  digitalWrite(redLed, LOW);
  digitalWrite(yellowLed, LOW); 
  digitalWrite(greenLed, LOW);

  oled.clear();
}

I know, I tried deleting this part from the code but it wont work without it.

 oled.setFont(FONT6X8);

  oled.setCursor(13, 2);
 
  oled.print(F(""));
  
  oled.setCursor(16, 3);
  
  oled.print(F(""));

You don't power the Tiny (at least there is no power in the wiring diagram you posted). I'm surprised that you see something on the OLED with that wiring. As pin 7 is used for SCL it must be pulled high which is probably done on the OLED board. Maybe that pullup is enough to drive the board?

As you failed to specify the ATtiny core you're using you might also read the wrong pin (3). Remember the different ATtiny cores have different pin numberings!

I am supplying the 5V, pin 8, and ground, pin 4 from the arduino. Can the zener diode get a reading from the positive end of the battery if its connected to pin 7? Or does it need to be connected to a different pin? I tried each pin. Is this the part of the code that needs changing?

analogValue = analogRead(2);

Notice that analogRead(2) is actually pin 7 on the attiny. I am also using board: ATtiny 45/85 (Optiboot)

I am supplying the 5V, pin 8, and ground, pin 4 from the arduino.

Which Arduino? I cannot see an Arduino in the wiring diagram.

Notice that analogRead(2) is actually pin 7 on the attiny. I am also using board: ATtiny 45/85 (Optiboot)

As I wrote before: Which core are you using to program the ATtiny? The standard Arduino IDE cannot program an ATtiny so you must have extended it with a corresponding core. As the pin assignment is different for the available cores we must know which core you using!

SOLVED!!! So I had to modify the code. Instead of individual color LED's, I had to use an RGB_LED. I am only displaying green, yellow and red colors so the blue part of the RGB is not connected to anything. I am using an arduino mega 2560 to power the attiny. See attached file.

Here is the complete code:

#include <Tiny4kOLED.h>


int redPin = 1;
int greenPin = 3;

int analogValue = 0;
float voltage = 0;
int ledDelay = 1000;
// ============================================================================

void setup() {
   oled.begin();

    oled.clear();

  // Turn on the display
  oled.on();

    oled.switchRenderFrame();
 pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  
}

void loop() {
   oled.setFont(FONT8X16);

    oled.setCursor(32, 0);

    oled.print(F("Voltage"));

    oled.setFont(FONT6X8);

    oled.setCursor(13, 2);

    oled.print(F(""));

    oled.setCursor(16, 3);

   oled.print(F(""));

    oled.switchFrame();

  delay(10);
analogValue = analogRead(A2);
  voltage = 0.0048*analogValue;
 oled.println(voltage);
  
  if( voltage >= 1.6 )
    setColor(0, 255, 0);  //green LED color
  else if (voltage > 1.2 && voltage < 1.6)
     setColor(255, 255, 0);  // yellow LED color
  else if( voltage <= 1.2)
    setColor(255, 0, 0);  // red LED color

    oled.clear();
}

void setColor(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  //analogWrite(bluePin, blue);  
}

I also discovered that the zener diode needs to be A2 in the code and it is connected to pin 3 on the attiny. A1 will not work, I tried it. This was the part that was giving me a headache but trial and error and hours and hours and hours of work paid off.

analogValue = analogRead(A2);

Now on to create the gerber for this!

One last question, how can I modify the code so that the attiny can properly read the voltage on a 9V battery? The above code will only read 9V batteries as 4.91V. Instead of using 0.0048*analogValue, what other number can be used to properly display 9V batteries?

voltage = 0.0048*analogValue;

Hi,
Why are you using a zener instead of a potential divider to scale your voltage reading?

Using a zener and a multiplier in your code to scale will not give you an accurate voltage range.

Tom.... :slight_smile:

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