Go Down

Topic: Tone Library and Digital Potentiometer - Weird results (Read 3020 times) previous topic - next topic


I'm attempting to use the tone library and a digital potentiometer to programmatically change the volume of my tones.
I have not even hooked up the digital potentiometer yet. When I call the subroutine to enable volume change, I shouldn't get any volume change at all because I haven't hooked up the chip yet. I expect to get a tone in each ear and a corresponding delay for a total repeating duration of about 4 seconds. This works correctly in the left ear. However, in the right ear, I get a rising, then falling pitch for the same duration as the left ear. This may have something to do with the limitations of the Atmega 328. I'd be grateful if someone could explain why what I'm doing does not give expected results, and whether there is a workaround that will work with the digital potentiometer I've chosen.
Code: [Select]
Sketch: Tone_Test_With_Digital_Potentiometer
Author: Chris Sparnicht - http://low.li
Date: 2011.01.31
License: Creative Commons 2.5 Attrib. & Share Alike
Description: A binaural tone test for arduino.
Note: Make sure you have dual wheel potentiometer to reduce
the volume of the audio with your headset. If you don't,
you might damage your ear drums, your arduino or your headset.

Include the arduino tone library (you'll have to download
this from arduino.cc's playground, since it's not standard.
#include <Tone.h>
//Variables for tone generator
float binauralBeat[] = { 14.4, 11.1, 6.0, 2.2 };
Tone rightEar;
Tone leftEar;
float centralTone = 200; //We're starting at this tone and spreading the binaural beat from there.
int volumeRight = 3;
int volumeLeft = 11;

Setup defines pins and tones.
void setup()  {
  pinMode(volumeRight, OUTPUT);
  pinMode(volumeLeft, OUTPUT);

Main function - play the tones continuously in a loop
void loop()
  for (int i = 0; i < 4; i++){
  rightEar.play(centralTone - (binauralBeat[i]/2));
  leftEar.play(centralTone + (binauralBeat[i]/2));
  // Serial.print("Right Ear: ");
  Serial.print(centralTone - (binauralBeat[i]/2));
  Serial.print(" | ");
  // Serial.print(" | Left Ear: ");
  Serial.println(centralTone + (binauralBeat[i]/2));
//   delay(3000);

void crescendoDecrescendo(){
  for (int j = 0; j < 256; j++){
    analogWrite(volumeRight, j);
    analogWrite(volumeLeft, j);
  for (int j = 255; j > 0; j--){
    analogWrite(volumeRight, j);
    analogWrite(volumeLeft, j);


I shouldn't get any volume change at all because I haven't hooked up the chip yet.

Yes but you are using software timers, the same that the Tone Library uses. The only way round this I can think of is to write your own digital pot interface code so that it doesn't need the internal timers.


I know this is a little late for a reply, but there is a solution, as long as you only use 2 tones.

The first two tones created will use timers 2 and 1.  This means that Timer 0 is still configured such that you can use pins 5 & 6 as PWM outputs.

Change your Volume pins to 5 & 6 and you should be good to go.

http://wiring.org.co/ - Wiring - Where Arduino came from. (Wiring begat Arduino)

http://loftypremises.com/ - Everything you wanted to know about me.... and less

http://roguerobotics.com/ - #1 Canadian Arduino distributor

Go Up