Musical Bug

Introduction

Everybody loves a good tune, even CodeBug! You can program CodeBug to play music using a buzzer or speakers. Musical notes are generated on CodeBug by turning an output leg on and off very quickly at different frequencies depending on the note. You can control what note is played and for how long. By putting notes together, you can create tunes, make your own synthesisers or even make sounds for games.

You will need
CodeBug CodeBug
Micro USB cable
Computer
Crocclips
OR Headphones
Buzzer
10k ohm potentiometer (optional)

Writing your program

To turn CodeBug into a music maker, you are going to program it to play notes from one of its legs. Follow the tutorial below to write your program.


Attaching your noise maker

You can make music with CodeBug using a sounder or headphones/speakers and crocclips. First connect a black crocclip to the GND leg on CodeBug and a coloured crocclip to leg 0 on CodeBug.

To play the music through headphones or a speaker, clip the black crocclip that is connected to the GND leg, to the metal part of the headphone plug that is closest to the plastic cover. Now connect the coloured crocclip to the tip of the headphone plug. There should be a middle metal part of the plug that is not touching either crocclip.

Warning: This will be very loud, do not put the headphones into your ears.

To play music through a sounder, connect the black crocclip to one leg of the sounder and the coloured crocclip to the other leg of the sounder.


Controlling volume

You will find that the volume of the music will be too loud when using headphones. You can change the volume by adding a resistor in series between leg 0 and the headphone tip. Larger resistor values will make the music quieter.

If you want to be able to vary the volume, you can add a potentiometer. Make sure you have followed this activity on using potentiometers. Disconnect your coloured crocclip from your headphones/buzzer and connect it to the power leg of the potentiometer. Now connect another crocclip from the output leg of the potentiometer to the tip of the headphone plug (or free leg of the buzzer). Finally connect the ground leg of the potentiometer to the GND leg on CodeBug.


What next

Try some of these other simple tunes such as Happy Birthday, Twinkle Twinkle Little Star or Swan Lake.


Back to top