This hardware-based Pulse-Width-Modulation (PWM) library enables you to use the Hardware-PWM on Arduino AVR ATtiny85-based boards (Digispark) using ATTinyCore. The Timer/Counter1 is used in asynchronous mode to give a PWM-output on Pin PB1 or Pin PB4 up to 500 kHz. The maximum resolution is 8-bit, the effective resolution typically between 7-bit and 8-bit.
I needed a hardware PWM for a DigiSpark-board to drive a fan at 25kHz and didn't found a library supporting the ATTinyCore.
If you have one of the boards and need a flexible hardware-based PWM, this library might also fit for you. Otherwise, I'm still happy to use it ;).
See the provided example on how to use the library.
--
A bare minimum example is given below:
#include "DigiSpark_PWM.h" // https://github.com/soylentOrange/DigiSpark_PWM
// Create instance of DigiSpark_PWM-class, connected to Pin-PB1
// (This Pin is connected to the onboard LED)
DigiSpark_PWM pwm = DigiSpark_PWM(PIN_PB1);
// For connecting to Pin PB4 simply use:
// DigiSpark_PWM pwm = DigiSpark_PWM();
// begin PWM-output
void setup() {
// initialize and start PWM-output @1Hz with 50% duty-cycle
pwm.begin(1, 50);
}
void loop() {
// nothing to do here, the LED will blink driven by the hardware PWM
}
The example can be obtained from within the Arduino IDE in File->Examples->DigiSpark_PWM->DigiSpark_PWM_example.
This function initializes the library. Call before use...
Initial frequency (in Hz) and duty cycle (in percent) are given here.
The funcion will return an error if PWM is unavailable:
- ERROR_INVALID_PIN (0x02) - pwm is not supported on the pin,
- or 0 if everything went well.
Dynamically set the duty cycle (in percent). The funcion will return an error if PWM is unavailable:
- ERROR_NOT_INITIALIZED (0x01) - pwm is not initialized yet,
- ERROR_INVALID_PIN (0x02) - pwm is not supported on the pin,
- or 0 if everything went well.
Dynamically set the freuqncy of the pwm (in Hz). The duty cycle will match the prevous setting. The funcion will return an error if PWM is unavailable:
- ERROR_NOT_INITIALIZED (0x01) - pwm is not initialized yet,
- ERROR_INVALID_PIN (0x02) - pwm is not supported on the pin,
- or 0 if everything went well.
Returns the pin given during instanciation.
Open up the Arduino Library Manager
in the Arduino IDE and search for DigiSpark_PWM. Select / install the the latest version. Click this badge for more detailed instructions.
Press the green clone or download button in the upper-right and download as .ZIP. Then go to the Arduino IDE and go to Sketch>Use Library->Add .ZIP Library_ and select the just downloaded zip file.