moty
Published

Doorbell

Using EEPROM as a sound source.

BeginnerFull instructions provided19,462
Doorbell

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Resistor 2.21k ohm
Resistor 2.21k ohm
×2
Speaker: 0.25W, 8 ohms
Speaker: 0.25W, 8 ohms
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
General Purpose Transistor PNP
General Purpose Transistor PNP
×1
Capacitor 22 µF
Capacitor 22 µF
×1
Pushbutton switch 12mm
SparkFun Pushbutton switch 12mm
×1
24C512
×1

Story

Read more

Schematics

bell_eeprom

Code

bell

C/C++
/*
 * eeprom_bell.ino
 *
 * Created: 30/06/2017 23:34:47
 *  Author: moty22.co.uk
 */ 
#include <Wire.h>


const int audio = 6;     //output D6
const int ring = A2;     //ring pushbutton
int ringPB=1;         // ring pushbutton status

void setup() {
  // put your setup code here, to run once:

pinMode(ring, INPUT_PULLUP);

  //PWM Timer0
OCR0A = 64;
TCCR0A = _BV(COM0A1) | _BV(WGM01) | _BV(WGM00);  //output in phase, fast PWM mode
TCCR0B = _BV(CS00); // 16MHz/256 = 64KHz

Wire.begin();        // init i2c bus
Wire.setClock(200000);  //200khz

}



void loop()
{
    unsigned int i;
    
    ringPB=digitalRead(ring);
    if(ringPB==LOW)   //pushbutton
     {
      pinMode(audio, OUTPUT);
          //go to address 0
      Wire.beginTransmission(0x50);
      Wire.write((int)(0));   //MSB of eeprom address
      Wire.write((int)(0)); // LSB
      Wire.endTransmission();


       //start sequential read
      TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); //start command
      while (!(TWCR & (1<<TWINT)));   //till interrupt 
        //write address
      TWDR = 0b10100001;    //address+read
      TWCR = (1<<TWINT)|(1<<TWEN);
      while (!(TWCR & (1<<TWINT)));   //till interrupt 
    
          //read all the bytes
      for (i=0; i<0xfff0; i++)
      {
         //send ACK
        TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
        while (!(TWCR & (1<<TWINT)));
        OCR0A =  TWDR;      //PWM = eeprom data
      }  
    
        //send NACK, no-aknowledgement + stop commands ends reading
      TWCR = (1<<TWINT)|(1<<TWEN);
      while (!(TWCR & (1<<TWINT)));
      OCR0A =  TWDR;  
      
        //stop
      TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);  

      pinMode(audio, INPUT);    //stop the audio output 
     }
     
}

Credits

moty

moty

12 projects • 83 followers

Comments