# Overview
Recently Great Scott built his [DIY version of a tachometer](https://youtu.be/6QZMt4yyylU) which I thought was very cool. But using an ATmega for this job, I found a bit overpowered. So I tried to force all tasks (measurement, calculation, I²C protocol and OLED display) into the huge 1KByte memory of an ATtiny13.
- Firmware (Github): https://github.com/wagiminator/ATtiny13-TinyTacho
- Project Video (Youtube): https://youtu.be/Iz7LjheLYKo

# Hardware
Since the ATtiny13 does almost all of the tasks, the wiring is pretty simple:

The IR LED emits light, which is reflected by the rotating object and detected by the IR photo diode. The photo diode changes its conductivity depending on the strength of the reflected light. If the rotating object has exactly one white stripe on an otherwise black surface, then the photo diode changes its electrical resistance twice per revolution and the voltage between the diode and the 10k resistor rises once above and falls once below a certain threshold, which is defined by the variable resistor.

If you want to use a coin cell to power the device, please remember that only the rechargeable LIR1220 Li-Ion batteries work. The "normal" CR1220s don't deliver enough power.
# Software
## Implementation
The IR photo diode is connected to the positive input of ATtiny's internal analog comparator, the variable resistor for calibration is connected to the negative input. An interrupt is triggered on every falling edge of the comparator output which saves the current value of timer0 and restarts the timer. The 8-bit timer is expanded to a 16-bit one by using the timer overflow interrupt. The saved timer value contains the timer counts per revolution. The RPM is calculated by utilizing the following equation:
```
RPM = 60 * F_CPU / prescaler / counter
= 60 * 1200000 / 64 / counter
= 1125000 / counter
```
The calculated RPM value is displayed on an I²C OLED display. The I²C protocol implementation is based on a crude bitbanging method. It was specifically designed for the limited resources of ATtiny10 and ATtiny13, but should work with some other AVRs as well. The functions for the OLED are adapted to the SSD1306 128x32 OLED module, but they can easily be modified to be used for other modules. In order to save resources, only the basic functionalities which are needed for this application are implemented. For a detailed information on the working principle of the I²C OLED implementation visit [TinyOLEDdemo](https://github.com/wagiminator/attiny13-tinyoleddemo).
```c
// global variables
volatile uint8_t counter_enable = 1; // enable update of counter result
volatile uint8_t counter_highbyte = 0; // high byte of 16-bit counter
volatile uint16_t counter_result = 0; // counter result (timer counts per revolution)
// main function
int main(void) {
uint16_t counter_value; // timer counts per revolution
uint16_t rpm; // revolutions per minute
PRR = (1
schematic diagram
(
1
/
)
Empty
12
22
Collect to album