Can't Get accurate voltage reading on Attiny85 Pin using a voltage divider

Hi there and thank you for helping,

I have been working with microcontroller for a couple of years now, mainly Arduino Uno/Nano, ESP32 and Attiny85.
With the latter i am trying to build a fan controller that uses a 10k Potentiometer to control the spped of a 12vfan, meanwhile a WS2812 ring made of 12 LEDs is updating to indicate the speed level based on how many leds are on.
A voltage divider connect the signal from the potentiometer to the Attiny PB2 port in order to shift voltage. Everything works fine except for the fact that the voltage on PB2 is not totally stable and this creates a misreading, resulting in the Ws2812 leds blink or turning on faultly, any ideas on how to stabilize the aforementioned voltage? Thank you

//#include <SoftwareSerial.h>
#include <util/delay.h>
#include <Adafruit_NeoPixel.h>
// Clock at 8mHz
#define F_CPU 8000000  // This is used by delay.h library
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1:
#define LED_PIN    4

// How many NeoPixels are attached to the Arduino?
#define LED_COUNT 12
const int FETPin = 1;  // Only works with Pin 1(PB1)
const int POTPin = A1; // Potentiometer Pin
// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

void setup()
{
  // Serial.begin(9600); // send serial data at 9600 bits/sec
  pinMode(FETPin, OUTPUT);
  pinMode(POTPin, INPUT);
  strip.begin();           // INITIALIZE NeoPixel strip object (REQUIRED)
  strip.show();            // Turn OFF all pixels ASAP
}

void loop()
{ int in = analogRead(POTPin);
  if (in > 740) {
    digitalWrite(FETPin, HIGH);
  }
  else {
    digitalWrite(FETPin, LOW);
  }
  int y = map(in, 740, 830, 0, 12);
  for (int a = 0; a <= y; a++) {
    strip.setPixelColor(a, strip.Color(0,   0, 200));  //  Set pixel's color (in RAM)
  }
  for ( int b = y; b <= 12; b++) {
    strip.setPixelColor(b, strip.Color(0,   0, 0));  //  Set pixel's color (in RAM)
  }
  strip.show();                          //  Update strip to match
  //  Serial.println(in);
  //  Serial.println(y);
  _delay_ms(200);
}

That is what I would expect, you did not follow the manufacturer's recommendation on how to bypass the 7805. The leads must be kept very short, the caps should almost tough the regulator. Check your bypass caps it appears you are missing some.

Where did you get that schematic from? Why can't you just PWM the fan with the MOSFET?

Hi thank you for your responses,
@gilshultz
I have looked up the datasheeet for the lm 7805 and in fact an output capacitor of 10uf is advised, I must have lost a zero in my design :sleeping:. I am using ceramic capacitors for both the input and the output, do you think i should use an elctrolytic one for the input? I would try to implement your suggestion and report back.
@Jiggy-Ninja
I have tried PWM with a mosfet by changing the frequency output to 25kHz, but it didn't work as well as decreasing the voltage with the particular fan that i am using for this project

@gilshultz

I have tried to connect the capacitors as close as possible to the lm7805 regulator and i have changed the 0,01uf cap to a 0,1uf, but it did not help. I am getting a stable voltage out of the linear regulator, the unstable voltage comes from the voltage divider, I have tried changing the resistors of the voltage divider to higher values and that seems to slow down the unstability though the voltage keeps changing. I have also checked the voltage before the voltage divider and that's stable too. I really don't get what I am doing wrong.

Are you testing on a breadboard? As you said the voltage before the divider is stable. The breadboards I have often make poor contact and if I need it to be more accurate I have to solder it up on a perfboard first to test things out.

@hmeijdam
I have soldered all the components on a perfboard.

Looking at your schematic, I am confused about how the FET is wired. I would expect the gate to PB1.

You could try a capacitor over R3, if it is maybe high frequency pollution that the fan motor sends back into your system.

@hmeijdam
You are right I made a mistake in the schematics, but when i tested i had the mosfet wired correctly with the gate hooked to PB1. I think I solved by replacing resistor R3 with a 3.9 Volt zener Diode, this is outputting a stable voltage and it is aldo giving me a stable lowest voltage when the 10k pot is fully turned clockwise.

Screenshot from 2021-04-02 20-32-26.png

Screenshot from 2021-04-02 20-32-26.png

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