toothache

ATtiny13 TinyTacho

 
Overview
# Overview Great Scott recently made his [DIY version of a tachometer](https://youtu.be/6QZMt4yyylU), which I think is pretty cool. But using the ATmega for this job I found to be a bit too powerful. So I tried to force all the tasks (measurements, calculations, I²C protocol and OLED display) into the ATtiny13's huge 1KByte memory. * Firmware (Github): [https ://github.com/wagiminator/ATtiny13-TinyTacho](https://github.com/wagiminator/ATtiny13-TinyTacho) * Project video (Youtube): [https ://youtu. be/Iz7LjheLYKo](https://youtu.be/Iz7LjheLYKo) ![pic1.jpg](https://raw.githubusercontent.com/wagiminator/ATtiny13-TinyTacho/main/documentation/TinyTacho_pic1.jpg) # Hardware due to ATtiny13 Almost all tasks can be done, so wiring is very simple:

![Wiring.png](https://raw.githubusercontent.com/wagiminator/ATtiny13-TinyTacho/main/documentation/TinyTacho_Wiring.png) The infrared LED emits light, which is The rotating object reflects and is detected by an infrared photodiode. Photodiodes change their conductivity based on the intensity of reflected light. If the rotating object has exactly a white stripe on an otherwise black surface, the photodiode changes its resistance twice per revolution, the voltage between the diode and the 10k resistor rises once, and falls once below some threshold, which is determined by the variable Resistor definition. ![pic2.jpg](https://raw.githubusercontent.com/wagiminator/ATtiny13-TinyTacho/main/documentation/TinyTacho_pic2.jpg) If you want to use a coin cell battery to power your device, please remember that only the rechargeable LIR1220 lithium Ion batteries can work. The "normal" CR1220 doesn't provide enough power.


#Software## Implement the infrared photodiode connected to the positive input of the ATtiny's internal analog comparator, and the variable resistor used for calibration connected to the negative input. An interrupt is triggered on each falling edge of the comparator output, thereby saving the current value of timer0 and restarting the timer. The 8-bit timer is extended to a 16-bit timer by using the timer overflow interrupt. The saved timer value contains the timer count per revolution. RPM is calculated using the following equation: ``` RPM = 60 * F_CPU / prescaler / counter = 60 * 1200000 / 64 / counter = 1125000 / counter ``` The calculated RPM value is displayed on the I²C OLED display . The implementation of the I²C protocol is based on a rough displacement method. It is designed specifically for the limited resources of the ATtiny10 and ATtiny13, but should be used with some other AVRs as well. The OLED functionality is for the SSD1306 128x32 OLED module, but can be easily modified for use with other modules. To save resources, only the basic functionality required by this application is implemented. For more information on how the I²C OLED implementation works, please visit TinyOLEDdemo(https://github.com/wagiminator/attiny13-tinyoleddemo). ``` // 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< DIDR0 = (1< ACSR = (1< TIMSK0 = (1< sei( ); // enable all interrupts OLED_init(); // initialize the OLED // main loop while(1) { // loop until forever counter_enable = 0; // lock counter result counter_value = counter_result; // get counter result counter_enable = 1; // unlock counter result if (counter_value > 17) { // if counter value is valid: rpm = (uint32_t)1125000 / counter_value; // calculate RPM value OLED_printW(rpm); // print RPM value on the OLED } else OLED_printB(slow); // else print "SLOW" on the OLED } } // analog comparator interrupt service routine ISR(ANA_COMP_vect) { if(counter_enable) counter_result = (uint16_t)(counter_highbyte << 8) | TCNT0; // save result if enabled TCNT0 = 0; // reset counter counter_highbyte = 0; // reset highbyte TCCR0B = (1<} // timer overflow interrupt service routine ISR(TIM0_OVF_vect) { counter_highbyte++; // increase highbyte (virtual 16-bit counter ) if(!counter_highbyte) { // if 16-bit counter overflows TCCR0B = 0; // stop the timer if(counter_enable) counter_result = 0; // result is invalid } } `` ## Compile and upload due to circuit board There is no ICSP header on the PC, so before soldering using [SOP adapter](https://aliexpress.com/wholesale?SearchText=sop-8+150mil+adapter), or using [EEPROM clip](https://aliexpress .com/wholesale?SearchText=sop8+eeprom+programming+clip) After soldering, the ATtiny must be programmed. [AVR Programmer Adapter](https://github.com/wagiminator/AVR-Programmer/tree/master/AVR_Programmer_Adapter) can help with this. ### If using Arduino IDE * Make sure you have installed [MicroCore](https://github.com/MCUdude/MicroCore). * Go to **Tools->Boards->MicroCore** and select **ATtiny13**. * Go to **Tools** and select the following board options: * **Clock: **1.2 MHz Internal Oscillator* **Board: **Board Disabled* **Timing: **Microseconds Disabled * Connect your programmer to your PC and ATtiny. * Go to **"Tools"->Programmer**" and select your ISP programmer (eg [USBasp](https://aliexpress.com/wholesale?SearchText=usbasp)). * Go to** Tools->Burn Bootloader** to burn fuse. * Open TinyTacho.ino and click **Upload**. ### If using a precompiled hex file * Make sure you have [avrdude]( https://learn.adafruit.com/usbtinyisp/avrdude). * Connect your programmer to your PC and ATtiny. * Open a terminal. * Navigate to the folder containing the hex file. * Execute the following command (If necessary, replace "usbasp" with the programmer you are using): ``` avrdude -c usbasp -p t13 -U lfuse:w:0x2a:m -U hfuse:w:0xff:m -U flash :w:tinytacho.hex ``` ### If using makefiles (Linux/Mac) * Make sure you have installed the [avr-gcc toolchain and avrdude](http://maxembedded.com/2015/06/setting -up-avr-gcc-toolchain-on-linux-and-mac-os-x/). * Connect your programmer to your PC and ATtiny. * Open the makefile and change the programmer (if you are not using usbasp ). * Open a terminal. * Navigate to the folder containing the build files and sketches. * Run "make install" to compile, burn fuses and upload firmware. #Performance## Theoretical Considerations### Measurement range depends on: * Timer /Width of the counter (16 bits here) * Width of the result variable (16 bits here) * Clock frequency of the timer/counter (CPU clock/prescaler, here: 1.2MHz / 64 = 18.75 kHz) This Resulting in a measurement range of **17 to 62500 RPM**. In order to increase the measurement range, on the one hand the clock frequency of the timer must be increased by reducing the prescaler and/or increasing the CPU clock frequency, on the other hand the counter and the result must be The variable is extended to 32 bits. Additionally, the OLED_printW routine must be adjusted so that a 32-bit value can be displayed on the OLED. ### Measurement Resolution The measurement resolution depends on the resolution of the timer/counter, which is actually its clock Frequency. Due to the calculation formula, the RPM value is not proportional to the counter value, but rather exaggerated. This also means that the measurement resolution is not constant over the entire measurement range. Setting the timer/counter clock frequency to 18.75 kHz yields the following resolution based on the measured rotational speed: ![resolution.png](https://raw.githubusercontent.com/wagiminator/ATtiny13-TinyTacho/main/ The resolution in the figure shown in documentation/TinyTacho_resolution.png) represents the minimum distance between two measurements (the higher the value, the worse the resolution). To increase the resolution, the timer/counter clock frequency must be increased (see above). In order not to thereby reduce the measuring range, the counter and result variables must be extended to 32 bits. ### Measurement accuracy Measurement accuracy mainly depends on the accuracy of ATtiny’s internal RC oscillator. Per data sheet, factory calibrated to **+/-10%. **With [manual calibration](https://github.com/wagiminator/ATtiny84-TinyCalibrator) this can be improved to **+/-2%. **To get better values, it is necessary to use an accurate external clock signal, but this is beyond the purpose of this project. For higher RPM values, the latency of the interrupt service routine must also be considered. ## PRACTICE REVIEW### Sanity Check Simple sanity checks can be performed at lower speeds using video methods. More details can be found in Great Scott's video. Additionally, measurements can be compared to the manufacturer's motor specifications for measured RPM. TinyTacho passes both tests. ### Oscilloscope You can use an oscilloscope to measure the voltage behavior of the photodiode cathode. First, here it can be assessed whether there are uniform, clean and glitch-free waves, and thus whether passing white streaks can be reasonably detected. The frequency of the wave measured on the oscilloscope then automatically indicates the number of revolutions per second. If you multiply this value by 60 you will get the RPM and you can compare it to the value displayed on the OLED. In all measurements, the values ​​were within the theoretically predicted accuracy (without manual calibration of the ATtiny's oscillator). ![Scope 1.png](https://raw.githubusercontent.com/wagiminator/ATtiny13-TinyTacho/main/documentation/TinyTacho_scope1.png) ![Scope 2.png](https://raw.githubusercontent.com/ wagiminator/ATtiny13-TinyTacho/main/documentation/TinyTacho_scope2.png) ![scope3.png](https://raw.githubusercontent.com/wagiminator/ATtiny13-TinyTacho/main/documentation/TinyTacho_scope3.png) ![settings. png](https://raw.githubusercontent.com/wagiminator/ATtiny13-TinyTacho/main/documentation/TinyTacho_setup.png) ### Comparison with commercial equipment and cheap commercial tachometer [DT-2234C+](https://aliexpress .com/wholesale?SearchText=dt-2234C), showing the same results as the oscilloscope. ![Commercial.jpg](https://raw.githubusercontent.com/wagiminator/ATtiny13-TinyTacho/main/documentation/TinyTacho_commercial.jpg) ## Bottom Line Even though TinyTacho is more of an educational and fun project, it delivers Reasonable readings, especially using a calibrated oscillator. It is much smaller and cheaper than commercial products. The TinyTacho is a useful measuring instrument if you can make very precise measurements without using high resolution.

# References, links and notes 1. [Great Scott's Tachometer](https://youtu.be/6QZMt4yyylU) 2. [ATtiny13 I²C OLED Tutorial](https://github.com/wagiminator/attiny13-tinyoleddemo) 3. [SSD1306 Datasheet](https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf) 4. [ATtiny13A Datasheet](http://ww1.microchip.com/downloads/en/DeviceDoc/doc8126.pdf) 5. [Calibrating ATtiny's Internal Oscillator](https://github.com/wagiminator/ATtiny84-TinyCalibrator) # License ![License.png](https://i.creativecommons.org/l/by-sa/ 3.0/88x31.png)

This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. ([http://creativecommons.org/licenses/by-sa/3.0/](http://creativecommons.org/licenses/by-sa/3.0/))
参考设计图片
×
 
 
Search Datasheet?

Supported by EEWorld Datasheet

Forum More
Update:2025-06-21 09:37:55

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
community

Robot
development
community

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号