Turning on and off modules with transistors

Hi, I'm making an FM radio and speaker bluetooth with an Arduino Nano.

The FM module I'm using is the TEA5767:

And the blutooth module I've connected comes from a cheap bluetooth dongle bought on amazon

I've opened it, connected power to the USB pins and get the audio output signal from the jack.

With Arduino I turn off and on each of these modules alternatively through two NPN transistors used as switches to drive current to bluetooth or radio.

There is also a rotary encoder, I use its button to switch between modules and the rotation to select the frequency for radio.

There is also a PAM8403 stereo amplifiers and two speakers.

Here is the complete schema:

Everything works fine except when I rise up the volume with the bluetooth dongle active, when I do this it turns off an then back on.

This doesn't happen when I turn up the volume with radio module.

If I connect the + of the bluetooth directly to the + of the dc power it works even if I rise up the volume to the max.

It seems to me that the blueetooth module doesn't receive enaugh current from the transistor when I rise up the volume.

I've used BC337-25 transistor.

Some ideas to fix it? Should I change the transistor? which one? Should I use a different way to switch on and off modules?

Thank you wise men. :slight_smile:
giulio

Hi,
Welcome to the forum.

Can you please post a circuit diagram, showing all your power supply connections and labelling all the pins on the components you have.
The fritzy picture you have supplied is not really informative, what are the E,B and C of the transistors.
You are lucky that all your devices use 5V supply, otherwise using NPN transistors for HIGH SIDE switching would be a problem.

Can you post your code using code tags please?

Thanks.. Tom.. :slight_smile:

Hi Tom,
here are more details. I've added all the labels to the fritzing, you shuold be able to understand, the pic is bigger and I've added a link to let you see the big image.

(Note: In the big image, I've removed the 220ohm resistor at the base of NPN on the left, which turns on and off the blutooth, because without resistor I can rise a bit the volume, within the 220ohm resistor it turns off when the music begins, just after my phone connects to the blutooth dongle)

Here is the code, but I think that the point isn't in the code because on low volume it works.

Look the "loop" function, it switches from radio to bluetooth using transistors when button on rotary encoder is pressed.

Tell me if you need more informations and thank you for your help Tom! :slight_smile:

int pinSwitchBT = 5;
int pinSwitchFM = 6;
int st = 0;   // 0=fm   1=blutooth
int oldvalbut = 1;

#include <avr/wdt.h>


#include <EEPROM.h>


int encoderPin1 = 2;
int encoderPin2 = 3;
int btfmTogglePin = 4;
volatile int lastEncoded = 0;    // x rotary encoder
volatile long encoderValue = 0;
long lastencoderValue = 0;
int lastMSB = 0;
int lastLSB = 0;
int dir = 0; // -1 giro ccw , 1 giro cw, 0 fermo

#include <TEA5767.h>          // https://github.com/andykarpov/TEA5767     >>> C:\Users\pons\Documents\Arduino\libraries\TEA5767-master
#include <Wire.h>

int bt = 1; // stato iniziale bottone

void storeFreq(double d) {
  // WRITE
  char s[5] = "_____";
  dtostrf(d, 1, 1, s);
  EEPROM.write(0, '!');
  for(int i=1;i<=5;i++){
    EEPROM.write(i, s[i-1]);
  }
}

double readFreq() {
  // READ
  char l[5] = "_____";
  char value = EEPROM.read(0);
  if(value=='!') {
    for(int i=1;i<=5;i++){
      value = EEPROM.read(i);
      l[i-1]=value;
    }
    l[5]='\0';
    double d;
    d=atof(l);
     return d; 
  } else {
    return 0;
  }
}

void goRadio() {

  double d = readFreq();
  if (d<1) d = 102.8;

  Serial.println("RADIOOOO");
  
  Wire.begin();

  TEA5767 Radio;
 
  int search_mode = 0;
  int search_direction;
  unsigned long last_pressed;

  Radio.init();
  Radio.set_frequency(d);


  while (st == 0) {

    unsigned char buf[5];
    int stereo;
    int signal_level;
    double current_freq;
    unsigned long current_millis = millis();

    if (Radio.read_status(buf) == 1) {
      current_freq =  floor (Radio.frequency_available (buf) / 100000 + .5) / 10;
      stereo = Radio.stereo(buf);
      signal_level = Radio.signal_level(buf);
    }

    if (search_mode == 1) {
      if (Radio.process_search (buf, search_direction) == 1) {
        search_mode = 0;
      }
    }

    if ( dir == 1) {
      search_direction = TEA5767_SEARCH_DIR_DOWN;
    } else {
      if (dir == -1) {
        search_direction = TEA5767_SEARCH_DIR_UP;
      }

    }
    if(dir!=0) {
        last_pressed = current_millis;
        search_mode = 1;
        Radio.search_down(buf);
            
        current_freq =  floor (Radio.frequency_available (buf) / 100000 + .5) / 10;
        stereo = Radio.stereo(buf);
        signal_level = Radio.signal_level(buf);

        Serial.print("FM: "); Serial.print(current_freq); Serial.print(" level: ");
        Serial.println(signal_level);

        storeFreq(current_freq);
    }

    if ( encoderValue > 255 ) {
      encoderValue = 255;
    }
    if ( encoderValue < 0 )   {
      encoderValue = 0;
    }

    dir = 0;

    bt = digitalRead(btfmTogglePin);

    if (bt == 0 && oldvalbut == 1) {

      Serial.println("PREMUTO2");
      if (st == 0) st = 1; else st = 0;
      if (st == 1) {
        Serial.println("bloutooth on");
        digitalWrite(pinSwitchBT, HIGH);
        digitalWrite(pinSwitchFM, LOW);
        delay(3000);
        return;
      }
    }

    oldvalbut = bt;

  }

}

void setup() {
  Serial.begin (57600);
  Wire.begin();

  pinMode(encoderPin1, INPUT);
  pinMode(encoderPin2, INPUT);
  pinMode(btfmTogglePin, INPUT_PULLUP);
  pinMode(pinSwitchBT, OUTPUT);
  pinMode(pinSwitchFM, OUTPUT);
  digitalWrite(pinSwitchBT, LOW);
  digitalWrite(pinSwitchFM, HIGH);

  digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
  digitalWrite(encoderPin2, HIGH); //turn pullup resistor on

  attachInterrupt(0, updateEncoder, CHANGE);
  attachInterrupt(1, updateEncoder, CHANGE);

  delay(3000);
  goRadio();

}

void loop() {

  bt = digitalRead(btfmTogglePin);

  if (bt == 0 && oldvalbut == 1) {

    Serial.println("PRESSED BUTTON");
    if (st == 0) st = 1; else st = 0;
    if (st == 1) {
      // TURN ON BLUTOOTH DONGLE
      // TURN OFF RADIO TEA5767
      Serial.println("bloutooth on");
      digitalWrite(pinSwitchBT, HIGH);
      digitalWrite(pinSwitchFM, LOW);
      delay(500);
      TWCR = 0;
      delay(2500);
    } else {
      // TURN ON RADIO TEA5767
      // TURN OFF BLUTOOTH DONGLE
      Serial.println("radio on");
      digitalWrite(pinSwitchBT, LOW);
      digitalWrite(pinSwitchFM, HIGH);
      delay(3000);
      goRadio();
    }
    Serial.print(encoderValue);
    Serial.print(" bt:");
    Serial.print(bt);
    Serial.print(" st:");
    Serial.println(st);

  }

  oldvalbut = bt;
}

void updateEncoder() {
  int MSB = digitalRead(encoderPin1); //MSB = most significant bit
  int LSB = digitalRead(encoderPin2); //LSB = least significant bit

  int encoded = (MSB << 1) | LSB; //converting the 2 pin value to single number
  int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

  if (sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) {
    encoderValue --;
    dir = -1; Serial.println("dir = -1");
  }
  if (sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) {
    encoderValue ++;
    dir = 1;Serial.println("dir = 1");
  }

  lastEncoded = encoded; //store this value for next time
}

The 5volt supply is connected to V-in (raw), which is the input of the onboard 5volt regulator of the Nano.
That regulator needs an input of 7-9volt to make a stable 5volt for the Nano.
The 5volt regulator on a Nano has a dropout voltage of ~1volt, so the Nano is currently running on ~4volt.
If you have a stable/regulated 5volt supply, then connect it to the 5volt pin (bypassing that regulator).

The modules are switched with NPN transistors in emitter follower configuration.
That means that they drop at least 0.7volt, and more if some current is drawn.
You must use PNP transistors for high-side switching (BC327).
Emitter to 5volt, collector to module supply, base resistor stays the same.
Data lines must be set to 'input' before you switch off power to the modules, otherwise the modules could still be phantom-powered through the data lines.
Leo..

Definitely don't use emitter follower - that's no use for switching as you've found out.

Switching is done with common-emitter circuit, use PNP for high-side switching, and always
use a base resistor, 220 or 150 should be good.

If the module you are switching needs more current you'll have to move to logic level MOSFET.

I've added all the labels to the fritzing, you shuold be able to understand, the pic is bigger

When you put lipstick on a pig it is still a pig. Can you do a schematic please.

MarkT:
Definitely don't use emitter follower - that's no use for switching as you've found out.

Can you please explain me why?

Because the output of an emitter follower can only ever be the voltage you put on the base minus 0.7V, so it is no good for switching or amplifying voltage.