Arduino code for ATtiny45

I am looking for a arduino source code for ATtiny45 to driving a servo motor.
Attiny45 control pin is PB3 (pin 2).
I need a header file (. h) and configure file (. cpp) and source code.

Have You looked among the existing IDE example codes?

What core do you have installed?

The ATTinyCore core comes with a version of the Servo library (Servo_ATTinyCore). I highly recommend the ATTinyCore. I tried many cores before settling on that core. It is the best.

After you install the core per the instructions on the git hub page, look in the IDE File, Examples for example code showing how to use the Servo_ATTinyCore library.

Hi, Ground Fungus,
Thank you for your advise.
Do you have Arduino project file (. ino) for ATtiny45 to drive servo motor?
If you have it, appreciate you provide to me as I reference.
Meanwhile, I will try your advice.

Thank you
Best Regards.

Any tiny85 code that I have would be written for the ATTinCore core. So you would have to install that core to run it. Once you install the core, in the IDE, just go to File, Examples, Servo_ATTinyCore and there are 2 examples to try (Knob, control a servo with a potentiometer and Sweep, the servo sweeps back and forth from 0° to 180°, 180° to 0°).

If you have further questions about servos with tiny85, let me know.

I think the "Servo. h" in sweep example is for Arduino Uno board.
The "Servo. h" not suitable for ATtiny45/85.
Another issue is, in sweep example, arduino Uno board, pin9 use as a servo motor control output pin.
In source code is "myservo. attach (9)
I want to use ATtiny44/85 pin2 (PB3) as a servo motor control pin.
So, can I change the to " my servo. attach (2)

Best Regards

"Any tiny85 code that I have would be written for the ATTinCore core."
Answer: In my case is I want to use tiny45.
I think your tiny85 code can use for tiny45 also.
Can you give your tiny85 code for me as a reference.

Thank you.

That is right. But the Servo_ATTinyCore library that comes with the ATTinyCore core is compatible with the tiny85 and the tiny45.

The (modified) knob example. Tested and works with my tiny85 ( I do not have a tiny45).

/*
   Controlling a servo position using a potentiometer (variable resistor)
   
   potentiometer wiper to A2 (physical pin 3.
   servo to pin 3 (physical pin 2).
   optional LED to pin 0 (physical pin 5).
   
   If Servo library is installed to libraries, then that version (which doesn't support ATtiny parts)
   will be used instead of the one included with the core, which does. To get around this,
   include  Servo_ATTinyCore.h instead - this will always use the version that came with core.
*/

#include <Servo_ATTinyCore.h>

Servo myservo;  // create servo object to control a servo

const byte potpin = A2;  // analog pin used to connect the potentiometer
const byte servoPin = 3;
const byte ledPin = 0; // optional

int val;    // variable to read the value from the analog pin

void setup()
{
   myservo.attach(servoPin);  // attaches the servo on pin 9 to the servo object
   pinMode(ledPin, OUTPUT);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 50; 
   if (millis() - timer >= interval)
   {
      timer = millis();
      val = analogRead(potpin);  // reads the value of the potentiometer (value between 0 and 1023)      
      val = map(val, 0, 1023, 10, 170);  // scale it to use it with the servo (value between 0 and 180)
      //digitalWrite(ledPin, !digitalRead(ledPin));
      analogWrite(ledPin, val);  // visual indication of pot reading OPTIONAL
       myservo.write(val);  // sets the servo position according to the scaled value
   }  
}

Thank you for your sharing.

I try to find the "Servo_ATtinycore. h" but can't get it.
Can you give me the link?

Thank you.

Did you install the ATTinyCore core?

My Attiny library I got it from other link that I found on Web.

The code that I posted was written for the ATTinyCore core. It probably will not work with any other core.

Finally, I am able to drive my servo motor.
Header file I used is servo8bits. h and servo8bits. cpp
Theses 2 files I got it on Web.
I found that the servo rotate speed is very slow even though the delay time I change to small value.
Any method to make the rotate speed faster?