Skip to content

Hardware-based Pulse-Width-Modulation (PWM) for AVR ATtiny85-based boards using ATTinyCore up to 500 kHz.

License

Notifications You must be signed in to change notification settings

soylentOrange/DigiSpark_PWM

Repository files navigation

DigiSpark_PWM

arduino-library-badge GitHub release GitHub GitHub issues


Abstract

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.


Table of Contents


Why do we need this library

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.

--

Example

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
}

Get example

The example can be obtained from within the Arduino IDE in File->Examples->DigiSpark_PWM->DigiSpark_PWM_example.


Functions

uint8_t begin(uint32_t frequency, uint8_t dutyCyclePercent)

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.

uint8_t setDutyCycle(uint8_t dutyCyclePercent)

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.

uint8_t setFrequency(uint32_t frequency)

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.

uint8_t getPin()

Returns the pin given during instanciation.


Installation

Library Manager

Open up the Arduino Library Manager in the Arduino IDE and search for DigiSpark_PWM. Select / install the the latest version. Click this badge arduino-library-badge for more detailed instructions.

Download

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.

About

Hardware-based Pulse-Width-Modulation (PWM) for AVR ATtiny85-based boards using ATTinyCore up to 500 kHz.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages