Tone() with 8-bit Volume Control - No Extra Components!

Using ultrasong PWM frequencies, the Arduino can now produce square tone() waves with 256 volume levels with nothing but a speaker!

Jul 16, 2016

39113 views

8 respects

Components and supplies

1

Arduino UNO

1

Speaker: 0.25W, 8 ohms

Project description

Code

Volume Library

The Volume library is also available through the Arduino Library Manager!

Downloadable files

Hookup Guide

Hookup Guide

Hookup Guide

Hookup Guide

Comments

Only logged in users can leave comments

Anonymous user

2 years ago

Hello, I am using an 8 ohm , 3W speaker connected between pin 5 & ground and am not seeing much of a difference in volume as compared to a simple output raised high with a 120ohm resistor in series with the speaker. I initialize using the following in setup: vol.begin(); vol.setMasterVolume(1.00); An in the loop I make the following call: vol.tone(tempvalue,255); Where tempvalue is a number from 100 to 6000. Any ideas where I went wrong?

DaveX

3 years ago

Neat trick. On the Arduino Mega, with its ATMega2560, there's an Output Compare Modulator OCM1C0A system that modulates between timer0 and timer1 like this so you could do it in hardware rather than software. The ATMEGA2560's OCM1C0A system modulates output on the PB7/OC0A/OC1C pin (Arduino Mega digital pin 13) using the OCR0A and OCR1C registers.

Anonymous user

5 years ago

Hello, I am using an 8 ohm , 3W speaker connected between pin 5 & ground and am not seeing much of a difference in volume as compared to a simple output raised high with a 120ohm resistor in series with the speaker. I initialize using the following in setup: vol.begin(); vol.setMasterVolume(1.00); An in the loop I make the following call: vol.tone(tempvalue,255); Where tempvalue is a number from 100 to 6000. Any ideas where I went wrong?