Arduino Pin Library v4.2.2
An easy to use Arduino library for fast and simultaneous operations on Arduino I/O pins.
Loading...
Searching...
No Matches
Arduino Pin Library

An easy to use Arduino library for fast and simultaneous operations on Arduino I/O pins. Supports Arduino AVR boards natively and custom boards by manually defining register addresses. Complete documentation can be found here. Source code can be found here.

Background

Background information about how direct port manipulation works can be found here.

Advantages

  • Much faster than built in digital functions
  • Easier to read than direct port manipulation
  • Allows for simultaneous operations on multiple pins
  • Portable across all Arduino AVR boards
  • Supports custom boards (Pin.getAnalogValue() and Pin.setDutyCycle(int value) not supported)

Benchmarks

Simple benchmark programs were created to compare this library to both the built in Arduino functions and direct port manipulation. The three benchmark programs are included in the examples/Benchmarks directory. Each program switches pin 13 between HIGH and LOW as quickly as possible. The program flash memory usage and the frequency of the generated square wave are used for comparison. Results:

Filename Flash Memory Usage Output Frequency
Arduino Arduino-Benchmark.ino 1,354 bytes 64.91 kHz
Pin Library Pin-Library-Benchmark.ino 1,164 bytes 532.3 kHz
Direct Port Manipulation Port-Manipulation-Benchmark.ino 648 bytes 2661 kHz

Usage

Install

Install from the Arduino Library Manager or download the latest release here.

Import Pin Library

Without support for simultaneous operations

#include <Pin.h>
Fast operations on Arduino I/O pins.

Create Pin Object

Single Pin

Pin myPin = Pin(5);
Class for fast operations on Arduino I/O pins.
Definition Pin.h:40

Array of Pins

Pin myPins[] = {6,7};

Use as Input

Set mode to input

myPin.setInput();
void setInput()
Set the pin mode to input.
Definition Pin.h:287

Enable pullup resistor

myPin.setPullupOn();
void setPullupOn()
Set the pin pullup resistor to on.
Definition Pin.h:297

Disable pullup resistor

myPin.setPullupOff();
void setPullupOff()
Set the pin pullup resistor to off.
Definition Pin.h:307

Use as Output

Set mode to output

myPin.setOutput();
void setOutput()
Set the pin mode to output.
Definition Pin.h:339

Set HIGH

myPin.setHigh();
void setHigh()
Set the pin output to HIGH.
Definition Pin.h:349
myPin = HIGH;

Set LOW

myPin.setLow();
void setLow()
Set the pin output to LOW.
Definition Pin.h:359
myPin = LOW;

Get Pin Info

Get mode (INPUT/OUTPUT)

myPin.getMode();
uint8_t getMode()
Get the mode of the pin from the DDR register.
Definition Pin.h:187

Get state (HIGH/LOW)

myPin.getState();
uint8_t getState()
Get the state of the pin from the PORT register.
Definition Pin.h:200

Get value (HIGH/LOW)

myPin.getValue();
uint8_t getValue()
Get the value of the pin from the PIN register.
Definition Pin.h:213
myPin == HIGH;
myPin == LOW;

Get analog value (0-1023)

uint16_t getAnalogValue()
Get the analog value of the pin.
Definition Pin.h:226

Toggle

Toggle mode (OUTPUT -> INPUT, INPUT -> OUTPUT)

myPin.toggleMode();
void toggleMode()
Toggle the pin mode (OUTPUT -> INPUT, INPUT -> OUTPUT)
Definition Pin.h:400

Toggle state (HIGH -> LOW, LOW -> HIGH)

myPin.toggleState();
void toggleState()
Toggle the pin state (HIGH -> LOW, LOW -> HIGH)
Definition Pin.h:410

PWM

Set duty cycle to ~50% (not every Pin supports PWM)

myPin.setDutyCycle(127);
void setDutyCycle(int value)
Set the PWM duty cycle.
Definition Pin.h:393

RC Timer

Special function specifically for timing using series resistor-capacitor circuits. Used to get analog information on digital pins. Connect the pin between the variable resistor (force sensitive resistor, photoresistor, etc.) and the capacitor. The function will charge the capacitor through the resistor and count down the timer until it is charged (50% voltage is considered charged). Finally, the capacitor is discharged to allow the function to be called again.

The function returns the value remaining on the counter when the pin state went to HIGH or 0 if the counter reached 0. The higher the resistance, the longer the capacitor will take to charge. Longer charge times mean the counter will be decremented more resulting in a lower value returned. Therefore, higher resistance -> lower value returned, lower resistance -> higher value returned.

Decrement a counter starting at 255 until the pin goes HIGH or the counter reaches 0

myPin.setLow();
myPin.rcTimer(255);
volatile unsigned int rcTimer(volatile unsigned int count)
Set the pin mode to input and decrement a counter until the pin goes HIGH or the counter reaches 0 th...
Definition Pin.h:424

Simultaneous Operations on Multiple Pins

All Pins in array must use the same DDR, PORT, and PIN registers. Look at the Arduino documentation for your board to determine what registers each pin uses. Because this library is built for speed, the Pin Group is not automatically checked to be valid for simultaneous operations. An invalid array will produce unexpected results without error, therefore it is highly recommended that the array be validated using the isValid() function during setup.

Import Pin Library with support for simultaneous operations

#include <Pin.h>
#include <PinGroup.h>
Simultaneous operations on Arduino I/O pins.

Create array of Pins for simultaneous operations

Pin myPins[] = {Pin(2),Pin(3),Pin(5)};
PinGroup myPinGroup = PinGroup(myPins);
Class for simultaneous operations on Arduino I/O pins.
Definition PinGroup.h:18

Check to ensure each Pin in PinGroup use the same registers

myPinGroup.isValid();
bool isValid()
Check the group to ensure all pins use the same registers.
Definition PinGroup.h:205

Simultaneously set mode for each Pin in PinGroup to INPUT

myPinGroup.setInput();
void setInput()
Set the pin mode to input.
Definition PinGroup.h:266

Simultaneously set mode for each Pin in PinGroup to OUTPUT

myPinGroup.setOutput();
void setOutput()
Set the pin mode to output.
Definition PinGroup.h:318

Simultaneously set state for each Pin in PinGroup to HIGH

myPinGroup.setHigh();
void setHigh()
Set the pin output to HIGH.
Definition PinGroup.h:328

Simultaneously set state for each Pin in PinGroup to LOW

myPinGroup.setLow();
void setLow()
Set the pin output to LOW.
Definition PinGroup.h:338

Simultaneously check if each Pin in PinGroup is HIGH

myPinGroup.getValue() == HIGH;
uint8_t getValue()
Get the value of the pin from the PIN register.
Definition PinGroup.h:189

Simultaneously check if each Pin in PinGroup is LOW

myPinGroup.getValue() == LOW;

Simultaneously set each Pin in PinGroup to its opposite mode

myPinGroup.toggleMode();
void toggleMode()
Toggle the pin mode (OUTPUT -> INPUT, INPUT -> OUTPUT)
Definition PinGroup.h:370

Simultaneously set each Pin in PinGroup to its opposite state

myPinGroup.toggleState();
void toggleState()
Toggle the pin state (HIGH -> LOW, LOW -> HIGH)
Definition PinGroup.h:380