📜 ⬆️ ⬇️

Electronic "ball of fate" on ATtiny13

image

Hey. Recently I reviewed one of my favorite films, namely “Route 60” with Amy Smart in the lead role. There, the protagonist had such a thing called the “ball of fate”, which answered him various questions. Well, after watching, I had a thought, but why not do something similar on a microcontroller, especially since I recently unlocked 4 pieces of ATtiny13, which I once blocked unknowingly what fyuzas are and what they eat with. Another ATtiny13 argument is the price, a very cheap microcontroller .

The size of the device can be estimated on the background of my palm, it turned out such a high-tech key chain.
')


In the end, I will definitely add not only the scheme, the files for Proteus 7, the sources, but also the fyuza, hex-file, so that everyone who knows how to use the programmer can repeat this device.

The code for ATtiny13 is written in the Arduino IDE, but due to the large consumption of resources of this IDE, which by the way are so severely limited in ATtiny13 (namely 1024 bytes for the code), I did not have enough memory, and then my initial knowledge of the microcontroller registers and meager experience with them, that's what happened:

View code
#define F_CPU 1200000UL //     #include <avr/io.h> #include <avr/sleep.h> //     #include <util/delay.h> #define led_Yes 0 // grn #define led_No 1 // red #define rand_gen 3 #define wait 5000 //       void setup() { //pinMode(led_Yes, OUTPUT); DDRB |= (1<<led_Yes); //pinMode(led_No, OUTPUT); DDRB |= (1<<led_No); } void loop() { randomSeed(analogRead(rand_gen)); //    byte randomValue; randomValue = random(0,2); //      0  1 if(randomValue > 0){ //digitalWrite(led_Yes, HIGH); PORTB |= (1<<led_Yes); } else{ //digitalWrite(led_No, HIGH); PORTB |= (1<<led_No); } _delay_ms(wait); system_sleep(); } void system_sleep(){ //digitalWrite(led_No, LOW); PORTB &= ~(1<<led_No); //digitalWrite(led_Yes, LOW); PORTB &= ~(1<<led_Yes); ADCSRA &= ~(1 << ADEN); //     ACSR |= (1 << ACD); //   //      set_sleep_mode(SLEEP_MODE_PWR_DOWN); //   -    while(1) { sleep_enable(); //   sleep_cpu(); // ! } } //    : 912  ( 1 024  ) 


How it works? The fact is that we are in the lines of randomSeed (analogRead (rand_gen)); we set a certain value that enters the microcontroller from the outside, since the microcontroller port is not connected to anything, that is, “hangs in the air”, then there is some white noise on it. For a microcontroller, it looks like this:

image

If the microcontroller does not output the expected response, you can reassign pins, that is, swap ports that are set using the directive #define led_Yes 0 and #define led_No 1, or else a simple way - to change places LEDs. The #define wait 5000 line is used to set the timeout for hibernation in milliseconds, that is, how long the LED will shine.

Let's see the consumption of the microcontroller in sleep mode in the datasheet:

image

As you see, the current is just 0.2 µA, with such a current, the battery itself will “sit down” faster than the microcontroller will discharge it by at least 1 percent.

In this code, a non-pseudo random number generator is used (the same sequence of numbers is always the same), this was achieved using the randomSeed () function.

The scheme is very simple:

image

A total of 6 components, not counting the CR2025 lithium battery. Resistors R1 and R3 are added purely for decency as they say, without them the circuit will work no worse than with them, although the resistor R1 slightly reduces the current of the LEDs, but is needed if the supply voltage of the circuit exceeds 3 volts.

I wonder how many clicks a CR2025 battery is enough, which capacity is about 150 mA / h by the way?

When a single button is pressed, the LED lights up for 5 seconds, that is, 60 seconds / 5 = 12 clicks per minute * 60 minutes = 720 continuous presses per hour, the circuit will consume 1.5 mA current per hour (measured by a multimeter consumption when the red LED is on), the capacity of 150 mA / h is 150 / 1.5 mA / h, that the circuit will work for 100 hours, since in an hour you can make a maximum of 720 clicks, then 720 * 100 = 72 000.

From a rough calculation, it turns out that until the CR2025 lithium battery is completely depleted, you need to make 72 thousand clicks, and this, moreover, if you press the button continuously, it will take 100 hours, which is a little more than 4 days.

The fuses are standard, namely those that are sewn into the microcontroller from the factory, if the ATtiny13 is fresh from the store, then nothing needs to be changed there with the fuses. I add, just in case, the screen from the calculator fyuzov :

image

UPD on the advice of one person, I added to the system_sleep () function the disabling of the analog comparator to save even more battery power. Files in the bottom of the article perezalito.

References:

Film Route 60 (Wikipedia) ;
Datashit on ATtiny13 ;
ATtiny13 firmware and programming with Arduino ;
Arduino IDE ;
Function randomSeed () ;
White noise ;
How to save space on the microcontroller? ;
Software that made a screenshot of noise - Serial oscilloscope ;
All files for this project (reloaded) ;
All my publications .

PS the circuit in Proteus will refuse to work, the same LED will constantly light up, since this program does not know how to emulate those pickups on the ADC port that will be in the real circuit.

Source: https://habr.com/ru/post/241728/


All Articles