ARDUINO UNO ACTS WEIRD with audio and LEDs with external power supply

im building a "magical box" using an arduino UNO, a speaker, an SD Module and some LEDs.

For now the design and functioning is simple:

  1. A magnetic switch (SW1) is put in the box. When it opens it activates a first TIP41C transistor that will provide 8-9V to the arduino VIN pin and an 7805 5V voltage regulator.
  2. Once opened, the arduino is turned on and starts playing a song saved in an SD card. At the same time, 12 LEDs driven by 3 TIP41C are turned on. They have 3 patterns that can be selected with an external button (not shown in diagram, connected to pin 2.

Everything works fine when I use an external 5V source on a breadboard and power the NANO with USB. However, when trying to use and external 9V connected in a jack, it works normally if the LEDs doesnt have to change pattern and stay always on (default pattern). When a different sequence is selected, everytime they have to "blink" the music acts weird, and sounds like if you had a radio with bad signal trying to tune in. I dont know why this happens and only seems to happen with external power supply.

Any idea why this happens or how i can improve it?
Also, not 100% sure of the design itself arround the TIP41 and 7805, really could use some tips there too.

Code and schematic (done in proteus, used terminals for the SD module and NANO pins) shown below.

Thanks!!!!

#include <SD.h>              // need to include the SD library
#define SD_ChipSelectPin 10  //connect pin 4 of arduino to cs pin of sd card
#include <TMRpcm.h>          //Arduino library for asynchronous playback of PCM/WAV files
#include <SPI.h>             //  need to include the SPI library

TMRpcm tmrpcm;  // create an object for use in this sketch
int trans1 = 3;
int trans2 = 5;
int trans3 = 6;
int sensorCAP = 2;
volatile int ledstates = 0;



void setup() {
  tmrpcm.speakerPin = 9;  //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
  pinMode(trans1, OUTPUT);
  pinMode(trans2, OUTPUT);
  pinMode(trans3, OUTPUT);
  pinMode(sensorCAP, INPUT);
  attachInterrupt(digitalPinToInterrupt(sensorCAP), nextLed, RISING);

  Serial.begin(9600);
  Serial.println("Initializing....");
  delay(1000);
  SD.begin(SD_ChipSelectPin);
  File root;
  root = SD.open("/");

  printDirectory(root, 0);

  Serial.println("done!");
  tmrpcm.setVolume(5);  //
  char archive = "song.wav";
  tmrpcm.play("song.wav");  //the sound file "song" will play each time the arduino powers up, or is reset
}

void loop() {


  if (!tmrpcm.isPlaying()) {

    tmrpcm.disable();
  }

  Serial.println(ledstates);

  switch (ledstates) {
    case 1:
      // statements
      patron1();

      break;
    case 2:
      patron2();

      break;
    case 3:
      patron3();

      break;
    default:
      digitalWrite(trans1, HIGH);
      digitalWrite(trans2, HIGH);
      digitalWrite(trans3, HIGH);
      break;
  }
}



void printDirectory(File dir, int numTabs) {

  while (true) {

    File entry = dir.openNextFile();

    if (!entry) {

      // no more files

      break;
    }

    for (uint8_t i = 0; i < numTabs; i++) {

      Serial.print('\t');
    }

    Serial.print(entry.name());

    if (entry.isDirectory()) {

      Serial.println("/");

      printDirectory(entry, numTabs + 1);

    } else {

      // files have sizes, directories do not

      Serial.print("\t\t");

      Serial.println(entry.size(), DEC);
    }

    entry.close();
  }
}

void patron1() {
  if (ledstates != 1) { return; }
  digitalWrite(trans1, HIGH);
  digitalWrite(trans2, HIGH);
  digitalWrite(trans3, HIGH);
  if (ledstates != 1) { return; }
  delay(2000);
  if (ledstates != 1) { return; }
  digitalWrite(trans1, LOW);
  digitalWrite(trans2, LOW);
  digitalWrite(trans3, LOW);
  if (ledstates != 1) { return; }
  delay(2000);
}
void patron2() {

  if (ledstates != 2) { return; }
  for (int j = 0; j < 3; j++) {

    for (int i = 0; i < 255; i++) {
      analogWrite(trans1, i);
      analogWrite(trans2, i);
      analogWrite(trans3, i);
      if (ledstates != 2) { return; }
      delay(5);
    }
    for (int i = 255; i > 0; i--) {
      analogWrite(trans1, i);
      analogWrite(trans2, i);
      analogWrite(trans3, i);
      if (ledstates != 2) { return; }
      delay(5);
    }
    if (ledstates != 2) { return; }
    delay(2000);
  }
}

void patron3() {
  int linea = 1;
  int espacio = 2;
  int punto = 3;
  morse(linea);
  morse(espacio);
  morse(punto);
  morse(punto);
  morse(linea);
  morse(espacio);
  morse(linea);
  morse(punto);
  morse(linea);
  morse(punto);
  morse(espacio);
  morse(linea);
  morse(linea);
  morse(linea);
  morse(espacio);
  morse(punto);
  morse(linea);
  morse(punto);
  morse(punto);
  morse(espacio);
  morse(punto);
  morse(linea);
  if (ledstates != 3) { return; }
  delay(2000);
}


void morse(int lineaespaciopunto) {

  switch (lineaespaciopunto) {
    case 1:
      // linea
      digitalWrite(trans1, HIGH);
      digitalWrite(trans2, HIGH);
      digitalWrite(trans3, HIGH);
      if (ledstates != 3) { return; }
      delay(1500);
      digitalWrite(trans1, LOW);
      digitalWrite(trans2, LOW);
      digitalWrite(trans3, LOW);
      if (ledstates != 3) { return; }
      delay(1500);

      break;
    case 2:
      // espacio

      digitalWrite(trans1, LOW);
      digitalWrite(trans2, LOW);
      digitalWrite(trans3, LOW);
      if (ledstates != 3) { return; }
      delay(500);

      break;
    case 3:
      // punto

      digitalWrite(trans1, HIGH);
      digitalWrite(trans2, HIGH);
      digitalWrite(trans3, HIGH);
      if (ledstates != 3) { return; }
      delay(750);
      digitalWrite(trans1, LOW);
      digitalWrite(trans2, LOW);
      digitalWrite(trans3, LOW);
      if (ledstates != 3) { return; }
      delay(750);
      break;
    default:
      // statements
      break;
  }
}


void nextLed() {
  delayMicroseconds(5000);

  if (ledstates > 2) {
    ledstates = 0;
  } else {

    ledstates = ledstates + 1;
  }
}

Transistors require properly calculated base resistors to function.
Apart from that, Q1 is not required as you already have the switch.

If you are using a regular rectangular 9V battery it probably can't put-out enough current and the voltage probably isn't "holding up". Do you have a multimeter to measure the voltage?

And yes, you should have a resistor (about 200 Ohms) in series with the transistor base.

im using a 9V -1A wall adapter. I measured and It gives constant 8V to arduino in VIN-GND. I was thinking maybe use a 9V -2A but 1A feels enough for this dont it?

Im adding resistors now.

Thats true lmao. Thnks, will change that.

nevermind i already had resistors. Just didnt update schematic

I took out Q1 as suggested and now is working fine. Not sure what happened, maybe wiring issues. Thank you all!

Not if you use them as emitter followers, like OP does.
Downside is that the LEDs now only get 5volt - 0.7volt = 4.3volt.

Q1 is also used as emitter follower, loosing 0.7volt.

I wouldn't have used transistors for the LEDs at all.
The LEDs are now set up for 3mA each, and the Uno has enough pins to drive them, even individually.

The 7805 is not an LDO, loosing another 2volt.
There are no caps on the 7805 either.

You could have used the 5volt regulator of the Arduino (no 7805 at all).

Why the SD card. You could have used tone() to play a melodie.

The speaker is driven again with an emitter follower, loosing 0.7volt drive.
Not bad there, but you could have used a base resistor and the pot/speaker in the collector line.

A 6volt battery (4 x AA) on V-in of the Uno would have been much better.
Leo..

im using an 9V power supply, so 7805 still delivers stable 5V to the rest of the circuit. I didnt want to use batteries at all. Its gonna be a gift and dont want it to be baterry dependant.

I originally planned to use the tone() option but it only plays, well, tones. With SD and PCM you can actually play the song itself.

Will try to use speaker on collector and see difference. Also im driving 12 LEDs, and already used some pins for other components so im not sure if i could drive them individually with arduino pins, thats why i used transistors.

thanks for feedback, Didnt really considered the voltage drops when using emmiter.

Makes sense.

The internal 5volt regulator of the Uno does a better job (lower dropout if needed),
so why not use that.

If you keep the emitter followers for the speaker and LEDs, then you can remove the 7805 and connect all collectors to the unregulated 9volt battery terminal. That won't change the LED current or speaker volume. Connect 9volt directly to V-in, to power the Uno and SD card only. Post the schematic if you want us to check.

Kudos for posting a clear schematic diagram.
You don't often see that on this site.

Do you know the analogue pins are just digital pins with the added functionality of analogue-in.
digitalWrite (A0, HIGH); // is valid
Leo..

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.