Home

Awesome

<div align = center>

ArduinoUtils

My utility collection for Arduino

Badge License: GPLv3     Badge Version     Badge Commits since latest     Badge Build Status     Badge Hit Counter <br/> <br/> Stand With Ukraine

Not yet available as Arduino library.

Button Install     Button API     Button Changelog

</div>

If you find this library useful, please give it a star.

🌎 Google Translate

<br/>

Table of content

SimpleEMAFilters

An EMA (Exponential Moving Average) filter behaves like an RC lowpass filter with RC = SamplePeriod((1-alpha)/alpha) see here.<br/> An EMA filter is implemented by e.g. the following statement:

int16_t sLowpass3;
int16_t Lowpass5;
...
sLowpass3 += ((aInputValue - sLowpass3) + (1 << 2)) >> 3; // 1.8 us, alpha = 0.125, cutoff frequency 22.7 Hz @1kHz
sLowpass5 += ((aInputValue - sLowpass5) + (1 << 4)) >> 5; // 2.5 us, alpha = 1/32 0.03125, cutoff frequency 5.13 Hz @1kHz

which takes 2.5 µs on a 16 MHz Arduino Uno.

The alpha's for the implemented ultra fast EMA filters are 1/2, 1/4, 1/8, 1/16, 1/32 and 1/256 corresponding to the shift values of 1, 2, 3, 4, 5 and 8.

For a 1 kHz sampling rate (1/1000s sampling interval) we get the following equivalent cutoff (-3db) frequencies:

Simplified formula for small alpha is CutoffFrequency = (SampleFrequency / (2π * ((1/alpha) - 1));

Using the more exact formula, alpha = 1 - e ^ -((2π * CutoffFrequency) / SampleFrequency)
=> CutoffFrequency = (-ln(1-alpha) * SampleFrequency) / 2π we get the values:

The maximum output value for integer filters is: InputValue - ((1 << (ShiftValue -1)) -1), i.e 85 for InputValue 100 and ShiftValue 5 (Lowpass5).

The SimpleEMAFilters.hpp contains:

All implemented filters are applied at once to the input test signal calling doFiltersStep(int16_t aInput) and the results can in turn easily be displayed in the Arduino Plotter.

Plotter outputs representing e.g. a 42, 21, 10.5, 5.2 Hz square / sine wave at a sample rate of 1 ms (or 84, 42 ... Hz at a sample rate of 0.5 ms and so on)

Arduino Plotter output for a rectangle input signal with a amplitude of +/- 100. E.g. LowP3 is the Lowpass with alpha 1/8 implemented by >> 3 and the cutoff frequency of 21 Hz. ArduinoPlotter output SignificantFilters

Note the following facts:

<br/>

Arduino Plotter output for a rectangle input signal with very low amplitude of +/- 20 where you see clipping effects due to the limited resolution of the used 16 bit math. ArduinoPlotter output LowPass_LowInput The Lowpass3_int32 and Lowpass5_int32 are 32 bit fixed point implementations for higher resolution which requires 4.2 µs instead of the 1.8 / 2.5 µs of 16 bit. <br/>

Arduino Plotter output for a sine input signal with a amplitude of +/- 100. ArduinoPlotter output for LowPass with sine input Note the different attenuations at different frequencies. <br/><br/>

Arduino Plotter output for a triangle input signal with a amplitude of +/- 100. ArduinoPlotter output triangle input Note that higher order filters and low pass with high shifts are almost a perfect sine.

Arduino Plotter output for a sawtooth input signal with a amplitude of +/- 100. ArduinoPlotter output for sawtooth input

Arduino Plotter output for a random input signal with a amplitude of +/- 100. ArduinoPlotter output for lowpass on random input

<br/>

Arduino Plotter output for a rectangle input signal with 4 different frequencies 42 Hz > 21 Hz > 10.5 Hz > 5.2 Hz

Click on the pictures to enlarge them.

All lowpass filters.Highpass filters.<br/>Generated by subtracting lowpass from input.
AllLowPassHighPassFilters
Bandpass and reject filters.<br/>Bandpass is generated by subtracting one lowpass from onother with different shift value.<br/>Reject filter is input minus bandpass.High order Lowpass.
BandPassAndRejectHighOrderLowPass
LowPass1, 3, 5 and 8.<br/>Lowpass8 behave like a integrator here, resulting in a triangle output.LowPass3, 5 and 8.<br/>Comparison of different implementations with 16 bit integer, 32 bit integer and 32 bit float.
LowPass1_3_5_8LowPass16And32Bit
BiQuad filters with damping factor = 0.<br/>You see the overshoot.
BiQuadFilters

The floating point implementation of the 1/32 EMA filter takes 24 to 34 µs.<br/> There are convenience functions implemented for EMA filter, but normally it is better to implement it inline by e.g.

int16_t Lowpass3;
int16_t Lowpass5;
int32_t Lowpass5_int32;
..
Lowpass3 += ((InputValue - Lowpass3) + (1 << 2)) >> 3; // 1.8 us, alpha = 0.125, cutoff frequency 22.7 Hz @1kHz
Lowpass5 += ((InputValue - Lowpass5) + (1 << 4)) >> 5; // 2.5 us, alpha = 1/32 0.03125, cutoff frequency 5.13 Hz @1kHz
Lowpass5_int32 += ((((int32_t) InputValue) << 8) - Lowpass5_int32) >> 5; // Fixed point 4.2 us, value is Lowpass5_int32 >> 8
Lowpass8_int32 += ((((int32_t) InputValue) << 16) - Lowpass8_int32) >> 8; // Fixed point 2.0 us because of fast shift, value is Lowpass8_int32 >> 16

!Attention! The 16 bit implementations are limited to a maximum input value of +/- 16383 for rectangular input (which is the worst input case). The reason is, that the term InputValue - Lowpass3 must always fit into a 16 bit signed integer.

Related Links

ADCUtils

Fast and flexible ADC conversions. Intelligent handling of delays for reference and channel switching.

HCSR04

You can modify the HCSR04 modules to 1 Pin mode:

  1. Old module with 3 16 pin chips:<br/> Connect Trigger and Echo direct or use a resistor < 4.7 kΩ. If you remove both 10 kΩ pullup resistor you can use a connecting resistor < 47 kΩ, but I suggest to use 10 kΩ which is more reliable.
  2. Old module with 3 16 pin chips but with no pullup resistors near the connector row:<br/> Connect Trigger and Echo with a resistor > 200 Ω. Use 10 kΩ.
  3. New module with 1 16 pin and 2 8 pin chips:<br/> Connect Trigger and Echo by a resistor > 200 Ω and < 22 kΩ.
  4. All modules:<br/> Connect Trigger and Echo by a resistor of 4.7 kΩ.

MeasureVoltageAndResistance

Measures voltage and resistance with 1 mV and 2 Ω resolution at the lower end.<br/> First voltage is measured. If voltage is zero, then the unknown resistance to ground is measured using the 5 volt (VCC) supply with internal/series resistance of 10 kΩ or 100 kΩ.

Fritzing board

Fritzing board

Fritzing schematics

Fritzing schematics

BlinkLed

ShowInfo

HexDump

0x0000:  0xFF 0x81 0x82 0x00 0x08 0x02 0x00 0x27 0xFF 0xFF 0x0E 0xB3 0x81 0xFC 0x9B 0x47   .. .. '  .....G
0x0010:  0x00 0x00 0x00 0x00 0x20 0x65 0x00 0x0F 0xBE 0xEB 0x9B 0x98 0x2C 0xF1 0x08 0x2C       e .....,..,
0x0020:  0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55 0x55  UUUUUUUUUUUUUUUU

AVRUtils

RAM starts of variables initialized with values != 0, followed by variables initialized with 0 and variables not initialized by using attribute __attribute__((section(".noinit"))). It ends with the heap and the stack.

Sample output if stack runs into data

Size of Data + BSS, Heap start, Stack end=2041
Stack used 20 of 7
Currently available Heap=0

Optiboot 8.1

The optiboot 8.1 bootloader is required to determine the (watchdog) reset reason e.g. used in AVRUtilsDemo..

MillisUtils

Unifies millis() timer handling for Digispark, AttinyCore and Arduino cores.

DebugLevel.h

ATtinyUtils

AvrTracing

Tracing an Arduino program by printing each program counter value after executing one instruction.

Utilities available as separate Arduino library

ATtinySerialOut

Minimal bit-bang send serial

EasyButtonAtInt01

The very useful digitalWriteFast.h file based on the version from Watterott electronic.

Changing include (*.h) files with Arduino IDE

First, use Sketch > Show Sketch Folder (Ctrl+K).<br/> If you have not yet saved the example as your own sketch, then you are instantly in the right library folder.<br/> Otherwise you have to navigate to the parallel libraries folder and select the library you want to access.<br/> In both cases the library source and include files are located in the libraries src directory.<br/> The modification must be renewed for each new library version!

Modifying compile options with Sloeber IDE

If you are using Sloeber as your IDE, you can easily define global symbols with Properties > Arduino > CompileOptions.<br/> Sloeber settings

Revision History

Version 1.2.0

Version 1.1.0

Version 1.0.0

Version 0.8.0

CI

The library examples are tested with GitHub Actions for the following boards: