Play a Tune From ATTINY85 When Motion Detected

Introduction: Play a Tune From ATTINY85 When Motion Detected

As you can see from the video, a tune is played when movement is detected.

The program to do this runs on an ATTINY85 which is a very small microchip with great processing capabilities.

Movement is detected using a PIR sensor and the tune is played on a small passive speaker.

Here's what you will need

1. Breadboard.

2. Breadboard power supply set up for 3.3V. ( see photo)

3. ATTINY85.

4. Speaker with connections to breadboard. I soldered connectors onto mine.

5. PIR sensor for sensing movement. (see photo)

6. Breadboard connectors.

7. Capacitors - 104 for connecting between main vcc supply and vcc pin on ATTINY85, and 0.22uf capacitor (note long leg is positive) to place across + and gnd lines - see photo.

Setting up hardware is as per photo.

For the ATTINY85, find the small depression on the surface. This is where pin 1 is situated and the other 8 pins are situated clockwise to that. What is slightly confusing is that when referencing pins in the code, the PB codes are used not the pin numbers. These are shown as follows:

// *** Pinout ATtiny25/45/85:
// *** PDIP/SOIC/TSSOP // *** =======================GENERAL LAYOUT====================================================================== // *** // *** (PCINT5/RESET/ADC0/dW) PB5 [1]* [8] VCC // *** (PCINT3/XTAL1/CLKI/OC1B/ADC3) PB3 [2] [7] PB2 (SCK/USCK/SCL/ADC1/T0/INT0/PCINT2) // *** (PCINT4/XTAL2/CLKO/OC1B/ADC2) PB4 [3] [6] PB1 (MISO/DO/AIN1/OC0B/OC1A/PCINT1) // *** GND [4] [5] PB0 (MOSI/DI/SDA/AIN0/OC0A/OC1A/AREF/PCINT0) // *** // ATTINY 85 frequency set at internal 1 MHz

// *** =======================LAYOUT FOR THIS APPLICATION======================================================================

// *** NO CONNECTION PB5 [1]* [8] VCC // *** NO CONNECTION PB3 [2] [7] PB2 PIR SENSOR

// *** NO CONNECTION PB4 [3] [6] PB1 SPEAKER

// *** GND [4] [5] PB0 NO CONNECTION

So therefore, in the code when we assign a value to a pin, eg int Speaker = 1; we are referring to PB1 (which is actually pin 6). A few other things worth noting is that only PB0-PB4 tend to be used for programming and also certain pins have attributes and interchange between pins must be considered. For example, I found in an earlier program that when I assignedint Speaker to 0 (PB0) , I encountered errors.

Before discussing the complete hardware circuit, it is worth mentioning PIR sensors. They have two trimpots for time and sensitivity respectively. Please configure these as shown in my photograph above. Another point is that when switched on, PIR sensors take a minute or so to stabilize which can lead to initial unexpected behaviour.

Step 1: Setting Up the Circuit and Writing the Program.

The core of the circuit is the ATTINY 85 so I will use this as a reference point, See the attached photos.

I wrote the program using Arduino IDE 1.8.5 and internal 1 MHz, using a USBASP programmer. I assume that you know how to do this otherwise I have covered it, as have many, in other instructables. Copy and paste the code into the editor and burn it on to the ATTINY85. Note that the only part of the code you may wish to change are the tinynote values on the function playnote().

/* TinyTone for ATtiny85 */

// Notes these msu not be changed const int Note_C = 239; const int Note_CS = 225; const int Note_D = 213; const int Note_DS = 201; const int Note_E = 190; const int Note_F = 179; const int Note_FS = 169; const int Note_G = 159; const int Note_GS = 150; const int Note_A = 142; const int Note_AS = 134; const int Note_B = 127;

int Speaker = 1; // the pin to store the speaker int sensor = 2; // the pin that the sensor is attached to int state = LOW; int val = 0; // variable to store the sensor status void setup() { pinMode(Speaker, OUTPUT); pinMode(sensor, INPUT); // initialize sensor as an input delay(20000); // to allow pir to stabilise }

void loop() { val = digitalRead(sensor); // read sensor value if (val == HIGH) { // reading the data from the pir sensor

playTune(); } delay(5000); }

void TinyTone(unsigned char divisor, unsigned char octave, unsigned long duration) { TCCR1 = 0x90 | (8-octave); // for 1MHz clock // TCCR1 = 0x90 | (11-octave); // for 8MHz clock OCR1C = divisor-1; // set the OCR delay(duration); TCCR1 = 0x90; // stop the counter }

// Play a scale void playTune(void) { //a minuet tune - this section may be changed TinyTone(Note_A, 4, 1500); TinyTone(Note_G, 4, 1500); TinyTone(Note_A, 4, 1500); TinyTone(Note_G, 4, 1500); TinyTone(Note_F, 4, 1500); TinyTone(Note_CS, 4, 1500); TinyTone(Note_A, 4, 1500); TinyTone(Note_G, 5, 1500); TinyTone(Note_GS, 4, 1500); TinyTone(Note_F, 4, 1500); TinyTone(Note_CS, 4, 1500); TinyTone(Note_AS, 4, 1500); TinyTone(Note_F, 4, 1500); TinyTone(Note_G, 4, 1500); }

Step 2: Conclusion

The application is simple but effective and ought to give you ideas for other projects. I hope you have enjoyed it.

Share

    Recommendations

    • Metalworking Contest

      Metalworking Contest
    • Water Contest

      Water Contest
    • Creative Misuse Contest

      Creative Misuse Contest

    Discussions