Please save this noob! ATtiny85 + PWM = Sad Times

Howdy nerds (I have the utmost respect for everyone in this forum. You are all smarter than me)

I am an absolute beginner with this stuff. I've been paying a nice Indian chap on Fiverr to help me out with a project and only recently have I lost some confidence.

I am at a point where I am unable to progress due to some issues with my application. I am somewhat locked on hardware because it was already designed an manufactured with pcbway (probably a mistake, but I trusted that ample testing was done by said Fiverr friend)

Anyway... I'm trying to create a small board that will take a 12V power input and a 12V signal. When no signal is present (resting state), it needs to read a potentiometer to output a PWM to a mosfet (to covert the 5v back to 12) powering some LEDs.
When a signal IS present, it will simply set pwm to 100% duty then go back to resting state when signal is no longer present. (with some delay built in to ignore some noise in the signal)

So here is where I am with the code. I'm sure it isn't the most elegant, but it seems simple to me. The LED I am powering is capable of drawing 3A. Even with the pot at the lowest setting, I'm not able to have it draw less than 2A... for whatever reason, the combo of the attiny85, the mosfet, and the pot is not doing the same thing as my Wokwi simulation. Measuring the pot with my multimeter it is clearly 0-5v. I have tried ignoring the pot all together, manually setting the pwm output to a low value like 5 or 10 (out of 255 theoretically unless I've missed something) with no change to the floor. Can't get it to draw any less power aka use a lower duty cycle. I would really like to figure out how to get that 0-100% range.

I'm leaning toward something about the clock settings/time/frequency/duty of the attiny pwm to mosfet relation is not correct. I'm already so over my head on this though. Everything I've typed above (and below) I've literally learned in the last 3 hours of googling. I have no experience with any other Arduino.

// Board : Attiny85

#define sig 3
#define pot A2
#define out 1

int count = 0;

void setup() {
  pinMode(sig, INPUT);
  pinMode(pot, INPUT);
  pinMode(out, OUTPUT);
}

void loop() {
  delay(100);
  bool signal = digitalRead(sig);

  if(signal){
    count++;
  }else{
    count = 0;
  }

  if (signal && count >= 10) {
    count = 10;
    digitalWrite(out, 1);
  } else {
    int pwm = map(analogRead(pot), 0, 1023, 1, 255);
    analogWrite(out, pwm);
  }
}

Thanks for looking! -Chris

Please provide a schematic.
A multimeter is not necessarily appropriate for measuring PWM voltage or current.

// Board : Attiny85

#define sig 3
#define pot A2
#define out 1

void setup() {
  pinMode(out, OUTPUT);
}

void loop() {
  delay(100);

  if (digitalRead(sig))digitalWrite(out, 1);
  else analogWrite(out, analogRead(pot) >> 2);
}

Sorry if it wasn't clear... I was only testing the potentiometer output to make sure it was not faulty. That is still proper I believe to view 0-5v.

Schematic of the actual pcb may be a bit tricky to source. I've attached a picture that I have. I'll see if I can find something better... basically it mostly is resistors and such to take the 12V down to 5V, then back up to 12 after the fet.

All other changes you made will not work as far as I can tell.. it removes the ability to filter noise in the signal line (full power 20ms pulses at 2.5-3Hz)

As for the write, what does the >>2 do there? Looks like you're setting the pwm value to be some relation of the potentiometer value, but I can't quite follow. Remember, noob here haha

UPDATE: I added the else portion as a replacement of my own just to try it - it did not help. It was able to hit 0 power draw, but then an immediate spike to 2A. Assuming duty cycle is linear with current draw... I basically have 0% and ~60%-100%. Hunting down that allusive 1-59% haha

Show the picture of the ATtiny85 Breakout Board you are using. Is it Digispark ATtiny85 Dev Board (Fig-1)?
DigisparkAttiny85Board

Figure-1:

No I am using the SparkFun Tiny AVR programmer. The attiny85 is already installed on final assembly, so I'm using an alligator clip thing to reprogram on board.

The plot thickens - using the potentiometer on board, I'm getting my small bit of control with maybe 45° of rotation. the first 15-20° all hold 0 output, then the 45° that do something, and the final 180° or so are all at or near full duty.

Please, clear up the following points:
1. 12V supply is coming from a Battery or an AC-driven adapter?

2. Are you getting 5V for the ATtiny85 from 12V of Step-1 using a regulator?

3. When 12V of Step-1 is absent, are you powering ATtiny85 by a Battery? If yes, what is the voltage and Ah ratings of this Battery?

4. What is the input volatge of a your 5V-12V Buck Converter? Is it a Battery?

Please, show a block diagram of your system.

Power during testing right now is coming from a bench power supply. Set at 12.6V in final application it will come from a car at anywhere from 12V-13.8

Yes the 5V comes from that

If there is no power input the system is simply off. It is only on when the car would be running.

As for the diagram, see a previous comment from myself. That image is the best I have at the moment.

It appears quite simple and only 3 pins of the Attiny are used. If you haven't had the schematic then a reasonable resolution picture of both sides of the board should help.

A 12 volt LED at 2 or even 3 amps is going to be bright and hot. The FET driving it may also need a heat sink.

1. I think that the signal value is an indication of the presence of Bttery voltage -- is this correct? Or From where is coming this signal?

2. Why are you incrementig the count against the signal?

3. Your problem is this that you cacnot modulte the bigness of your LED -- is it correct?

4. You want tha the brightness of LED should depend on the value of Pot -- is it correct?

5. Is your hardware setup similar to Fig-1?


Figure-1:

Nice schematic. Which app did you use?

1 Like

I use Microsoft Visio App and draw the diagram manually.


2-3 amps is no problem on the chosen mosfet. I had a v1 of this board that worked perfectly in all regards except its ability to ignore signal noise that I didn't know was present. v2 introduces the attiny ONLY to be able to require the signal be on for a set period of time. In that introduction, we also are using the attiny to spit out the pwm.

Back to the issue... I'm pretty sure it must be something with the clock or prescale settings currently present on the attiny

  1. That is mostly correct. It comes from the vehicle as an auxiliary brake output wire. When signal is present it matches the battery voltage of the truck.
  2. Because of noise in the signal as stated in previous posts. I need the signal to have been present for 'a human input of pressing the brakes' amount of time to ignore this noise. (20ms pulses of full power at 2.5-3Hz)
  3. Correct
  4. Correct
  5. Relatively similar yes. I posted a front and back side view of the board above.
1 Like

The board layout in post #4 indicates that the mosfet is P channel because 1 terminal is connected to the positive (+12v) power rail in a high side switch configuration.
The main LED(s) will be connected between the terminal marked OUT and Ground (B-).

Should not the MOSFET be n-channel?

I've attempted to reverse engineer the signal part of your circuit, mainly from the board layout in post #4, and it looks like it will not do as you expect.
Only if the resistor (R?) (marked with a red ring below) has got a very high value or is omitted altogether has it a chance of working.

Also to note:

  1. The internal pullup resistor of pin2 of the ATtiny85 must be switched on.
    pinMode(sig, INPUT_PULLUP);

  2. The reading will be inverted. LOW if a signal is present and HIGH if not.

Can you identify the following component values marked with a red ring ?

EDIT
To your original problem, maybe it is that you did not prepare the ATtiny85 fuses for the Arduino environment.

This text

#### Timing or baud rates are wrong

Typically 8x slower than expected - see below entry.

#### When using an individual chip for the first time, or after changing the clock speed, EESAVE or BOD settings, 
you must do "burn bootloader" to set the fuses, even if you are not using the chip with a bootloader

Is taken from here: GitHub - SpenceKonde/ATTinyCore: Arduino core for ATtiny 1634, 828, x313, x4, x41, x5, x61, x7 and x8

Google for "Mosfet High Side Switch" to see how it is configured. Anyway, it is normal in a vehicle to switch on the high side, where the chassis is always ground (at least has been since about the latter half of the last century). You only have to run one wire to the light or other electrical fitting because that fitting can be grounded locally.