PWM with ATTiny 85

I've been putzing around with this most of the day and I'm sure it's something simple...

I'm trying to get a coded PWM value from my ATtiny to drive a mosfet which will drive a nano muscle.

Here's the driver:

I'm making good progress on this project but this has me stuck.

I'm trying this code

/*
 Fade
 
 This example shows how to fade an LED on pin 9
 using the analogWrite() function.
 
 This example code is in the public domain.
 
 */
int brightness = 0;    // how bright the LED is
int fadeAmount = 1;    // how many points to fade the LED by

void setup()  { 
  // declare pin 9 to be an output:
  pinMode(0, OUTPUT);
} 

void loop()  { 
  // set the brightness of pin 9:
  analogWrite(0, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                            
}

with an LED but it's funky. The LED does fade but it blinks while doing so. It's a pretty neat effect but not what I was looking for :stuck_out_tongue:

I've tried just using analogWrite with different values and it still blinks.

Any input? Fooling with these little stand alone chips is REALLY neat! And I'm getting a real kick out of it!

It's the delay(30);
Isn't it?
[You can see that.]

If I take the delay out I just get blinking with no dimming, at least none I can see.

This:

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

void loop()  { 
  
  analogWrite( 0,155);    

  
     
                              
}

Also results in a blinking LED. Shouldn't it just result in a dimmer LED?

Do you have a current limiting resistor on that LED?

I have verified fade in/out with the following:

int BRT = 0;    // how bright the LED is
boolean increase = true;

void setup()  { 
  // declare pin 9 to be an output:
  pinMode(0, OUTPUT);
} 

void loop()  
{ 
  if (increase == true)
  {
    BRTup();
  }
  else
  {
    BRTdown();
  }
}

void BRTup()
{
  if (BRT < 255)
  {
    BRT++;
  }
  else
  {
    increase = false;
  }
  illuminate();
}  

void BRTdown()
{
  if (BRT != 0)
  {
    BRT--;
  }
  else
  { 
    increase = true;
  }
  illuminate();
}

void illuminate()
{
  analogWrite(0,BRT);
  delay(20);
}

It's by no means the "last word". Just how I saw it.
The perception of fading/variation will depend on the LED used.

Sorry guys, and thanks for the help!

I'm a 'tard :disappointed_relieved:

Seems that my 12V power supply was causing some weirdness :blush: This stand-alone stuff introduces it's own set of troubles, especially when other stuff is on the same circuit and AC comes into play...

Here's what I'm fooling with:

It's a NM70R-2P nano muscle in case anyone is wondering. The MAD5 driver is zip-tied to it.

This is the code running it:

int pin         =  0;     // LED connected to PWM pin 11  
int pulsewidth  =  20;    // Any value between 0 and 255  
  
void setup() {  
  // None required for analogWrite!  
}  
  
void loop() {  
  analogWrite(pin, pulsewidth); 
 delay(3000);
analogWrite(pin, 0);
delay(3000);
}

When the LED is illuminated the muscle is "flexing" and it's "relaxing" under it's own power (A spring I suppose) when the LED is off. You can see in the video that there isn't a lot of rotation. I needed the PWM because without it it just "snaps" into position when 5V is placed on the gate.

The migamotors site has some info on driving them with an Arduino.

Sorry about posting before I had exhausted all options :blush:

Now I need t shrinkify my voltage regulator. Any ideas on that? I haven't even began looking... And a small battery. I'm thinking one from an electric cigarette would work well. I imagine power consumption/duty cycle would be similar.

Thanks again for the help!

hoff70:
Now I need t shrinkify my voltage regulator. Any ideas on that? I haven't even began looking... And a small battery. I'm thinking one from an electric cigarette would work well. I imagine power consumption/duty cycle would be similar.

Thanks again for the help!

Not sure what a "nano muscle" is but:
Getting a smaller voltage regulator is absolutely no problem. They are available in a variety of packages including TO-92 or small surface mount packages with various current handling capacities. Also if you use a battery with less then 5.5v output, you do not need a voltage regulator.