Arduino+ buzzer making fun little music

Source: Internet
Author: User
Tags ming
Arduino related poses

Arduino is an open source hardware platform that includes development kits (development boards and related modules as well as wiring) and Ides, and the development language is based on C. The core is a 8-bit ATmega328 processor, this processor is the AVR MCU series of high-end products. The Arduino provides a digital interface and an analog signal port, as well as an expansion port for easy design. 5V and 3.3V voltages can be generated when the board is powered on. buzzer

According to the principle of sound is divided into voltage-type buzzer and electromagnetic buzzer. We use the electromagnetic buzzer, after the power supply, vibrating diaphragm periodic vibration sound. One thing to be aware of is that the audible noise is an instantaneous magnetic field in the interior that causes the diaphragm to vibrate. if the inside has been a direct current, we can only hear a little bit of sound, if the need for continuous sound (like my birthday before the kind of songs to sing the little Thing) should do. bingo! need an AC drive circuit on the line. The simplest square wave can be driven. Here we use a passive buzzer , here is the source refers to the oscillator source, the buff is the source, that is, directly with the direct current can be audible, the passive need to send the external square wave signal for the driver. Hardware Connection

The connection is simple, the positive and negative poles are directly connected to the digital IO and GND respectively. If you feel too loud, you can concatenate a resistor of around 1K (depending on your buzzer's rated voltage).

let the buzzer ring.

We have connected the digital IO to the positive of the buzzer, so that the buzzer can only be placed in the square wave on the digital IO.

Digitalwrite (Buzzerpin,high); High
-level delay (+)
; Digitalwrite (buzzerpin,low);  Low
-level delay (100);

One of 100 refers to 100ms, so that the power (high) duty cycle is 50%, the period of 200ms, then the frequency is 500Hz, this frequency is actually relatively high. different melodies. principle

If this is the case, you can simply send out an alarm type of sound. What to do if you want to emit precise notes of different frequencies. does it have to be counted.

Well, I'm sure I know that different tones have different frequencies, and a tone frequency map is posted here.

In the Arduino we can use the tone () function to achieve the effect of the sound, you can refer to this link.

Method

The realization of music has two basic elements, the first is the note, just now I have taken care of, the second is the rhythm, that is, the tempo, or the value of the note. The previous delay () function can be used to extend the tone, so if we specify a delay of four minutes, we can control the extended time by writing a delay array.

It is worth noting that the delay () function is unsigned long, so the delay cardinality of the four note may be set to an even number so that the other notes can be halved and doubled. This is handy if you don't have too many legato and less-time-value notes. Code

Play the small stars everybody will, here put a song very like Clannad's "Chao Ming" Small paragraph code, counted as this small project gift ~ (~ ̄▽ ̄) ~

/** * Use buzzer to play Chao Ming * @author mr.bubbles * 1=c * * #include "music_note.c"//note table int buzzer=3; Buzzer port, PWM int scale[]={g,a,ee,a,g,a,g,a,ee,a,g, a,ee,a,g,a,e, G,d,e,g,a,b, A,ee,a,
             G,a,g, A,ee,b,cc,b,cc,b,a,e, D,e,g,a,b,a,ee,a,g,a, G,a,ee,a,g,a,ee,a,g,a,  E,g,d,e,g,a,b,a,ee,a,g,a, G,A,EE,B,CC,B,CC,DD,EE,AA};
                  Melody float duration[]={2,1,1,1,1,7,1,1,1,1,1, 1,1,1,1,3,1, 3,1,1,1,1,1, 1,1,1,1,7,1, 1,1,1,1,1,1,1,1,6, 1,1,1,1,1,1,1,1,1,7, 1,1,1,1,1,1 , 1,1,1,3, 1,3,1,1,1,1,1,1,1,1,1,7, 1,1,1,1,1,1,1,1,1,4};  Note Value int len=0;
  The variable is later used to count the number of notes void Setup () {Pinmode (buzzer,output);
Len=sizeof (scale)/sizeof (scale[0]);
    } void Loop () {for (int i=0;i<len;i++) {tone (buzzer,scale[i]);
    Delay (250*duration[i]); Notone (Buzzer);
  Delay (100);
} delay (1000); }
ReferenceAll About Arduino uno (open source hardware) Arduino tone function buzzer output basic scale with Arduino play music Arduino playing little stars detailed principles elaborated, using Arduino to play gourd doll

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.