Audio and speaker problem

Hi there, I've attempted to make a speaker work from a force resistor, reading off an SD card. Problem is, everything in my sketch works except for the sound.

Could anyone give me a heads up as to why this isn't playing sound files?

Thank you, the sketch is below.

#include <SD.h>                      // need to include the SD library
//#define SD_ChipSelectPin 53  //example uses hardware SS pin 53 on Mega2560
#define SD_ChipSelectPin 10  //using digital pin 4 on arduino nano 328
#include <TMRpcm.h>
#include <SPI.h>
 TMRpcm tmrpcm;   // create an object for use in this sketch
char mychar;
/*This code allows a force resistor to dictate which Led will turn on.
Depending on the threshold, 3 alternating coloured Leds will turn on to signify
the power of a force. A 3 way switch turns the program to level 1, 2 or off.
*/
const int positionA = 2; // Switch position A
const int positionB = 3; // Switch position B
int ledPinA = 8;           // LED is connected to digital pin 7
int ledPinB = 7;           // LED is connected to digital pin 8
int ledPinC = 6;           // LED is connected to digital pin 9
int forceSensor = A0;         // Force sensor is connected to analog pin 0
int sensorValue;           // variable to store the value coming from the sensor
long previousMillis = 0;        // will store last time LED was updated
const int chipSelectPin = 10;
// the follow variables is a long because the time, measured in milliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 100;           // interval at which to blink (milliseconds)

void setup()
{
  pinMode(ledPinA, OUTPUT);   // sets the ledPin to be an output
  pinMode(ledPinB, OUTPUT);   // sets the ledPin to be an output
  pinMode(ledPinC, OUTPUT);   // sets the ledPin to be an output
  pinMode(positionA, INPUT); // sets the switch positon A as an input
  pinMode(positionB, INPUT); // sets the switch position B as an input
  pinMode (10, OUTPUT);      // sets the Sd card slot as an output
  tmrpcm.speakerPin = 9; //11 on Mega, 9 on Uno, Nano, etc
 
  Serial.begin(9600);           //initialize the serial port
  
  
  if (!SD.begin(SD_ChipSelectPin)) {  // see if the card is present and can be initialized: 
    return;   // don't do anything more if not
  }
}   

void loop()   // run over and over again
{
  int stateA = digitalRead(2);
  int stateB = digitalRead(3);


{
  sensorValue = analogRead(forceSensor);   // read the value from the sensor
  
    Serial.print(" Sensor Value: ");
  Serial.println(sensorValue);

  delay(100);   // delay for 1/10 of a second

unsigned long currentMillis = millis();

  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;  
    
    
    
  {if(stateA == HIGH && sensorValue>975){ //You can change the number here to adjust the threshold.
    Serial.print("HIGH");
    digitalWrite(ledPinC, HIGH); //Turn LED on
    tmrpcm.play ("Hit5c.wav");
    delay (4000); //Keep LED on for 4 seconds
    digitalWrite (ledPinC, LOW); //Turn LED off
    }
    else if( stateA == HIGH && sensorValue>700){ //You can change the number here to adjust the threshold.
    Serial.print("Medium");
    digitalWrite(ledPinB, HIGH); //Turn LED on
    tmrpcm.play ("Hit3c.wav");
    delay (4000); //Keep LED on for 4 seconds
    digitalWrite (ledPinB, LOW); //Turn LED off
    }
    else if( stateA == HIGH && sensorValue>300){ //You can change the number here to adjust the threshold.
    Serial.print("Low");
    digitalWrite(ledPinA, HIGH); //Turn LED 7 on
    tmrpcm.play ("Hit2c.wav");
    delay (4000); //Keep LED on for 4 seconds
    digitalWrite (ledPinA, LOW); //Turn LED off
  }

    {if( stateB == HIGH && sensorValue>700){ //You can change the number here to adjust the threshold.
    Serial.print("HIGH");
    digitalWrite(ledPinC, HIGH); //Turn LED on
    tmrpcm.play ("Hit6c.wav");
    delay (4000); //Keep LED on for 4 seconds
    digitalWrite (ledPinC, LOW); //Turn LED off
    }
    else if( stateB == HIGH && sensorValue>350){ //You can change the number here to adjust the threshold.
    Serial.print("Medium");
    digitalWrite(ledPinB, HIGH); //Turn LED on
    tmrpcm.play ("Hit1c.wav");
    delay (4000); //Keep LED on for 4 seconds
    digitalWrite (ledPinB, LOW); //Turn LED off
    }
    else if( stateB == HIGH && sensorValue>1){ //You can change the number here to adjust the threshold.
    Serial.print("Low");
    digitalWrite(ledPinA, HIGH); //Turn LED 7 on
    tmrpcm.play ("Hit4c.wav");
    delay (4000); //Keep LED on for 4 seconds
    digitalWrite (ledPinA, LOW); //Turn LED off
    }
    else {
    Serial.println("none");
    digitalWrite(ledPinA, LOW); //Turn LED off
    digitalWrite(ledPinB, LOW); //Turn LED off
    digitalWrite(ledPinC, LOW); //Turn LED off 
  }
}
}
}
}
}

How did you connect the speaker to the Arduino.
Did you use an amplifier?

An 8ohm speaker connected to an Arduino pin is almost a dead short, and could fry the pin.
Leo..

Wawa:
How did you connect the speaker to the Arduino.
Did you use an amplifier?

An 8ohm speaker connected to an Arduino pin is almost a dead short, and could fry the pin.
Leo..

I connected it by pin 9 going to a 4.7k resistor, onto the midde of a transistor. The E pin of the transistor goes to ground, and the C pin goes to the speaker, and then the speaker goes to ground.

Retserrof:
I connected it by pin 9 going to a 4.7k resistor, onto the midde of a transistor. The E pin of the transistor goes to ground, and the C pin goes to the speaker, and then the speaker goes to ground.

That will not work.

Best way is to use amplified computer speakers.
Use a 10k resistor and a 10-100uF capacitor in series between Arduino and amplifier input. +cap on Arduino side. Join grounds.

You can try that NPN transistor with 4k7 base resistor and a 100ohm resistor between +5volt and collector.
Connect -speaker to ground, and +speaker via a 100-470uF cap to collector. +cap on collector side.
Volume will be low and sound will be distorted.
Leo..

I'm actually having problems initializing the Sd card to make it work I think. I tried Quick start on SdFat and got:
errorCode: 0x1, errorData: 0xff

I also tried it on SdInfo and got cardBegin failed';

SD errorCode: 0X1
SD errorData: 0XFF

I double checked the sd car wiring, and there is nothing wrong with it. I'm using the Micro Sd card board from Adafruit.

Anyone? :slight_smile:

I connected it by pin 9 going to a 4.7k resistor, onto the midde of a transistor. The E pin of the transistor goes to ground, and the C pin goes to the speaker, and then the speaker goes to ground.

You've got no power to the speaker... Try disconnecting the ground connection from the speaker and connect the speaker to +5V instead.

You can check that with the tone() function.

(I can't help you with the SDcard problem.)

DVDdoug:
You've got no power to the speaker... Try disconnecting the ground connection from the speaker and connect the speaker to +5V instead.

A 5volt supply, and 8ohm speaker, and a small transistor trying to draw a few hundred milliamps through the speaker, something might smoke.
Leo..

Thanks everyone!

I was wondering if I could get the tone sketch, or music in general working on an Arduino Micro? I can get tone to work on my uno but the micro wont play the test tone?

Thanks

the micro wont play the test tone

What does the micro do instead?

jremington:
What does the micro do instead?

Okay so I just worked out it DOES play the test tone, just about 15 seconds later after reset, as opposed to straight away with the Uno. Thanks anyway :slight_smile: