Go Down

Topic: Problem with speaker and amplifier when using an I2C LCD (Read 1 time) previous topic - next topic

EverythingTechPro

Hello all,
I recently wired up a timer that uses an LM386 amplifier in combination with a 3 Ohm, 4 watt speaker. The speaker and amplifier work perfectly normally, unless I add in the code for the LCD, in which the speaker immediately starts humming and hissing. The LCD is a 20x4 screen that uses an I2C adapter. Does anyone know why this happens / how to fix it? Thanks

LM386 Module:

VCC -> 5v
IN -> Digital Pin 3 (PWM)
GND -> GND

20x4 I2C LCD

VCC -> 5v
GND -> GND
SDA -> A5
SCL -> A4
"Simplicity is the ultimate sophistication."

DVDdoug

You didn't show any code but probably the code that generates the audio is getting interrupted by the code that sends messages to the LCD display.


EverythingTechPro

Here's the code:

#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Talkie.h>

const byte address[6] = "00001";

const byte ROWS = 1;
const byte COLS = 3;

char text[8] = "";

char keys [ROWS] [COLS] = {
  {'u', 'd', 's'}
};
byte rowPins[ROWS] = {6};
byte colPins[COLS] = {5, 4, 2};

const uint8_t spREADY[]PROGMEM = {0x6A,0xB4,0xD9,0x25,0x4A,0xE5,0xDB,0xD9,0x8D,0xB1,0xB2,0x45,0x9A,0xF6,0xD8,0x9F,0xAE,0x26,0xD7,0x30,0xED,0x72,0xDA,0x9E,0xCD,0x9C,0x6D,0xC9,0x6D,0x76,0xED,0xFA,0xE1,0x93,0x8D,0xAD,0x51,0x1F,0xC7,0xD8,0x13,0x8B,0x5A,0x3F,0x99,0x4B,0x39,0x7A,0x13,0xE2,0xE8,0x3B,0xF5,0xCA,0x77,0x7E,0xC2,0xDB,0x2B,0x8A,0xC7,0xD6,0xFA,0x7F};
const uint8_t spSET[]PROGMEM = {0x08,0xF8,0x35,0x95,0x03,0x02,0xF8,0xC5,0x58,0x03,0x16,0xB0,0xC0,0x2A,0xA6,0x08,0x13,0xD7,0xCE,0xA7,0xEC,0xAE,0xD5,0xCC,0xD6,0xDC,0xEA,0x54,0x35,0xA6,0xA4,0xE5,0x9A,0x3D,0xCC,0x25,0x2E,0x08,0x14,0xA1,0xDC,0x45,0x64,0x03,0x00,0x30,0x60,0x88,0x30,0x05,0xFC,0x1C,0x25,0x80,0x65,0xB6,0x10,0x50,0xA2,0xD0,0xFF,0x03};

String M1, M2, S1, S2, s,          MM1, MM2, SS1, SS2, ss;
int m1, m2, s1, s2, S,             mm1, mm2, ss1, ss2, Ss;

String r1, r2, r3, r4, r5, r6;

unsigned long currentTime, subCurrTime;
unsigned long previousTime, subPrevTime;

int y = 1;
int z = 0;
char total[] = "";
String all;

boolean homeScreen = true;
boolean timing = false;
int lsr = 1;

LiquidCrystal_I2C lcd(0x27, 20, 4);
Keypad myKeypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
Talkie voice;
RF24 radio(7, 8);

void setup() {
  Serial.begin(9600);
  lcd.init();
  lcd.backlight();

  pinMode(3, OUTPUT);
 
  tone(3, 494, 100);
  delay(100);
  tone(3, 659, 100);
  delay(100);
  tone(3, 988, 100);

  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();

  lcd.clear();
 
  lcd.setCursor(4,1);
  lcd.print("Timetronics");
  lcd.setCursor(3,2);
  lcd.print("Timing Systems");

  delay(2000);
  lcd.clear();
 
  //checkWarnings();
 
  M1="0";
  M2="0";
  S1="0";
  S2="0";
  s="0";

  delay(100);
  hs();
}

void hs() {
  timing = false;
  homeScreen = true;
  lcd.setCursor(0,0);
  lcd.print("Choose an Option:");

  lcd.setCursor(1,1);
  lcd.print("Solo Timer");
  lcd.setCursor(1,2);
  lcd.print("Lap Timer");
  lcd.setCursor(1,3);
  lcd.print("Race Timer");

  lcd.setCursor(0,1);
  lcd.print(" ");
  lcd.setCursor(0,2);
  lcd.print(" ");
  lcd.setCursor(0,3);
  lcd.print(" ");
 
  lcd.setCursor(0,y);
  lcd.print(">");
}

void start() {
  //Beep beep (line up at start)
  tone(3,494,200);
  delay(400);
  tone(3,494,200);
  delay(5000);
  delay(random(0, 1000));

  //Ready
  voice.say(spREADY);
  delay(3000);
  delay(random(0, 1000));

  //Set
  voice.say(spSET);
  delay(random(2000, 4000));

  //Start!
  tone(3,330,750);
  radio.flush_rx();
  timing = true;
}

//SOLO
void soloTimer() {
  homeScreen = false;
  M1="0";
  M2="0";
  S1="0";
  S2="0";
  s="0";
  lcd.setCursor(0,0);
  lcd.print(M1);
  lcd.print(M2);
  lcd.print(":");
  lcd.print(S1);
  lcd.print(S2);
  lcd.print(".");
  lcd.print(s);

  start();

  lcd.setCursor(0,3);
  lcd.print(">Back");
}

//LAP
void lapTimer() {
  homeScreen = false;
  M1="0";
  M2="0";
  S1="0";
  S2="0";
  s="0";

  MM1 = "0";
  MM2 = "0";
  SS1 = "0";
  SS2 = "0";
  ss = "0";

  z = 0;
 
  start();

  lcd.setCursor(0,0);
  lcd.print("Total:");

  lcd.setCursor(0,3);
  lcd.print(">Back");
}

//RACE
void raceTimer() {
  homeScreen = false;
  M1="0";
  M2="0";
  S1="0";
  S2="0";
  s="0";
 
  z = 0;
 
  start();

  z = 0;

  lcd.setCursor(0,3);
  lcd.print(">Back");
}

void timer() {
  m1=M1.toInt();
  m2=M2.toInt();
  s1=S1.toInt();
  s2=S2.toInt();
  S=s.toInt();

  if(S>9) {
    S=0;
    s2=s2+1;
    s=String(S);
    S2=String(s2);
  }if(s2>9) {
    s2=0;
    s1=s1+1;
    S2=String(s2);
    S1=String(s1);
  }if(s1>5) {
    s1=0;
    m2=m2+1;
    S1=String(s1);
    M2=String(m2);
  }if(m2>9) {
    m2=0;
    m1=m1+1;
    M1=String(m1);
    M2=String(m2);
  }if(m1>9) {
    m1=0;
    M1=String(m1);
  }
  currentTime=millis();
  if(currentTime-previousTime>=95) {
    S=S+1;
    previousTime=millis();
  }

  M1=String(m1);
  M2=String(m2);
  S1=String(s1);
  S2=String(s2);
  s=String(S);
}

void subTimer() {
  mm1=MM1.toInt();
  mm2=MM2.toInt();
  ss1=SS1.toInt();
  ss2=SS2.toInt();
  Ss=ss.toInt();

  if(Ss>9) {
    Ss=0;
    ss2=ss2+1;
    ss=String(Ss);
    SS2=String(ss2);
  }if(ss2>9) {
    ss2=0;
    ss1=ss1+1;
    SS2=String(ss2);
    SS1=String(ss1);
  }if(ss1>5) {
    ss1=0;
    mm2=mm2+1;
    SS1=String(ss1);
    MM2=String(mm2);
  }if(mm2>9) {
    mm2=0;
    mm1=mm1+1;
    MM1=String(mm1);
    MM2=String(mm2);
  }if(mm1>9) {
    mm1=0;
    MM1=String(mm1);
  }
  subCurrTime=millis();
  if(subCurrTime-subPrevTime>=95) {
    Ss=Ss+1;
    subPrevTime=millis();
  }

  MM1=String(mm1);
  MM2=String(mm2);
  SS1=String(ss1);
  SS2=String(ss2);
  ss=String(Ss);
}

void loop() {
  char key = myKeypad.getKey();
 
  //Receive light sensor data
  if(homeScreen == true) {
    if(key == 'u') {
      y -= 1;
      if(y < 1) {
        y = 1;
      }
      hs();
     
    }else if(key == 'd') {
      y += 1;
      if(y > 3) {
        y = 3;
      }
      hs();
     
    }else if(key == 's') {
      if(y == 1) {
        lcd.clear();
        soloTimer();
      }else if(y == 2) {
        lcd.clear();
        lapTimer();
      }else if(y == 3) {
        lcd.clear();
        raceTimer();
      }
    }
   
  }else if(homeScreen == false) {
    if(key == 's') {
      lcd.clear();
      timing = false;
      hs();
    }
  }

  if(timing == true) {
    timer();
    if(y == 2) {
      subTimer();
    }
  }

  if (radio.available() && homeScreen == false) {
    radio.read(&text, sizeof(text));
    Serial.println(text);
    if(text[8] = "s") {
      if(y == 1) {
        timing = false;
        Serial.println("STOP");
       
      }else if(y == 2) {
        if(z == 0) {
          lcd.setCursor(10,0);
          lcd.print(String(M1) + String(M2) + ":" + String(S1) + String(S2) + "." + String(s));
         
        }else if(z == 1) {
          lcd.setCursor(10,1);
          lcd.print(String(MM1) + String(MM2) + ":" + String(SS1) + String(SS2) + "." + String(ss));
         
        }else if(z == 2) {
          lcd.setCursor(10,2);
          lcd.print(String(MM1) + String(MM2) + ":" + String(SS1) + String(SS2) + "." + String(ss));
         
        }else if(z == 3) {
          lcd.setCursor(10,3);
          lcd.print(String(MM1) + String(MM2) + ":" + String(SS1) + String(SS2) + "." + String(ss));
         
        }

        z++;
        if(z > 3) {
          z = 4;
           timing = false;
        }

        MM1 = "0";
        MM2 = "0";
        SS1 = "0";
        SS2 = "0";
        ss = "0";
       
      }else if(y == 3) {
        if(z == 0) {
          lcd.setCursor(0,0);
          lcd.print(String(M1) + String(M2) + ":" + String(S1) + String(S2) + "." + String(s));
         
        }else if(z == 1) {
          lcd.setCursor(0,1);
          lcd.print(String(M1) + String(M2) + ":" + String(S1) + String(S2) + "." + String(s));
         
        }else if(z == 2) {
          lcd.setCursor(0,2);
          lcd.print(String(M1) + String(M2) + ":" + String(S1) + String(S2) + "." + String(s));
         
        }else if(z == 3) {
          lcd.setCursor(10,0);
          lcd.print(String(M1) + String(M2) + ":" + String(S1) + String(S2) + "." + String(s));
         
        }else if(z == 4) {
          lcd.setCursor(10,1);
          lcd.print(String(M1) + String(M2) + ":" + String(S1) + String(S2) + "." + String(s));
         
        }else if(z == 5) {
          lcd.setCursor(10,2);
          lcd.print(String(M1) + String(M2) + ":" + String(S1) + String(S2) + "." + String(s));
         
        }

        z++;
        if(z > 5) {
          z = 6;
          //timing = false;
        }
      }
     
    }
  }

  if(homeScreen == false) {
    if(y == 1) {
      lcd.setCursor(0,0);
      lcd.print(M1);
      lcd.print(M2);
      lcd.print(":");
      lcd.print(S1);
      lcd.print(S2);
      lcd.print(".");
      lcd.print(s);
     
      lcd.setCursor(7,0);
      lcd.print(" ");
     
    }else if(y == 2) {
      lcd.setCursor(0,1);
      lcd.print(M1);
      lcd.print(M2);
      lcd.print(":");
      lcd.print(S1);
      lcd.print(S2);
      lcd.print(".");
      lcd.print(s);
     
      lcd.setCursor(7,1);
      lcd.print(" ");
    }
  }
}
"Simplicity is the ultimate sophistication."

MarkT

Hello all,
I recently wired up a timer that uses an LM386 amplifier in combination with a 3 Ohm, 4 watt speaker. The speaker and amplifier work perfectly normally, unless I add in the code for the LCD, in which the speaker immediately starts humming and hissing. The LCD is a 20x4 screen that uses an I2C adapter. Does anyone know why this happens / how to fix it? Thanks

The digital noise from switching the LCD backlight and signals is getting onto the supply rail and thus into the audio amp.  More and better decoupling of the supply to the amp will help.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

EverythingTechPro

How would I decouple the supply? With a capacitor?
"Simplicity is the ultimate sophistication."

Go Up