It is likely that the alarm clock does not provide any voltage level (I mean GND
) to the buzzer when it does not have to ring, the circuit is just left "open" then; it will provide a voltage level (depending on your clock power supply) only when the alarm has to ring.
On the other side, your Arduino pin 8 must have a voltage level that can be mapped to a 0
(anything between GND
and 5V/3
) or a 1
(anything between 2x5V/3
and 5V
).
However, in the absence of alarm, the input is left "dangling" (no voltage level at all). In CMOS technology, leaving an input pin dangling will make it randomly take 0
and 1
levels as you described.
Fortunately, the solution to this problem should be easy: you just have to force a 0
level to pin 8, by connecting it to GND
through a resistor. I generally use 10K or 15K for that resistor. This is called a "pulldown" resistor:
- when the alarm does not provide any voltage, the input pin level will be "pulled down" to
GND
, ie 0
.
- when the alarm provides a 5V (see important remark below) to the input pin, then it will be seen as
1
.
IMPORTANT: you have to make sure that your alarm clock, when ringing, provides a voltage that is not higher than 5V (it would grill your UNO, or more precisely the ATmega328 of your UNO) and is at least ~3V to be "recognized" as 1
.
NOTE: it is likely that solving this first issue will not be enough to make your complete program work, because your alarm clock probably does not send a continuous voltage to your buzzer but and alternate one to make it ring, hence you will get successive 1
and 0
at high speed (between 100Hz and 100Hz I would say) in your program. If that is the case please open another question as the solution for that specific issue is disconnected from the current one (random 1
and 0
).