0

Relay 'chatter' during MP3 audio playback
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Relay 'chatter' during MP3 audio playback

by cgrucelski on Sat Jun 30, 2018 12:07 am

Hi, all.

I've been tinkering with Arduinos for a couple of years, but am basically just a very part-time hobbyist. About once or twice a year, I have a small project that usually involves fairly simple sketches, such as watching for button input, then playing a random audio file.

The current project I am working on involves watching for a button press and then triggering a relay on, which will turn on an externally powered 12v light, playing a random audio clip, then triggering the relay off, which will turn the light back off again. I am using an Uno with the Adafruit "Music Maker" MP3 Shield (product ID: 1790). This is the relay I am using:

https://www.amazon.com/gp/product/B078478SZ9

The sketch I have is functional at this point, but the relay 'chatters' during audio playback. I do not have the external light connected at this point, as I am only testing the relay functionality. The connections between the Arduino and relay are as follows:

Arduino 'Gnd' to Relay 'GND'
Arduino '5v' to Relay 'VCC'
Arduino digital pin #12 to Relay 'IN1'
Momentary button wires connected to GPI02

When the momentary button is pressed, a random number is generated from the total number of audio clips and checked against the numbers already selected, to make sure clips aren't repeated. The relay is triggered on, there's a 2 second delay, the audio clip is played, there's another 2 second delay, and then the relay is triggered off.

The behavior I am seeing is that the relay is 'chattering' during playback of the audio clip. The small light on the relay board flickers noticeably and you can hear the relay clicking quietly, but rapidly. In my test set-up, I have a small LED bulb hooked up to the Arduino power for testing purposes and it does light up and stay solidly lit during audio playback.

I tried removing the jumper across the JD-VCC and VCC pins on the relay board and connecting a separate, external 5v power supply to the relay board, thinking that perhaps the draw on the Arduino's 5v power from both the 'Music Maker' shield and the relay board were too much to keep the relay solidly closed/open. But, that didn't make a difference.

The Arduino and relay board will be mounted in a location far enough away from the speakers that will be playing the audio clip, so I doubt anyone will be able to hear the relay 'chattering.' But, I wondered if anyone had any thoughts or ideas. The alternative may be for me to use a Mosfet (product ID: 355) for controlling the light instead of the relay.

Here's a quick video: https://photos.app.goo.gl/jVqbMkWdZcSiHGRT8

Here's the sketch:

Code: Select all | TOGGLE FULL SIZE
#include <SPI.h>
#include <Adafruit_VS1053.h>
#include <SD.h>

// pins used for the music maker shield
#define SHIELD_RESET  -1      // VS1053 reset pin (unused!)
#define SHIELD_CS     7      // VS1053 chip select pin (output)
#define SHIELD_DCS    6      // VS1053 Data/command select pin (output)

// pin used for the external relay board
#define RELAY1  12

// common pins between breakout and shield
#define CARDCS 4     // Card chip select pin
// DREQ should be an Int pin, see http://arduino.cc/en/Reference/attachInterrupt
#define DREQ 3       // VS1053 Data request, ideally an Interrupt pin

// define array of audio clips
#define ARRAYSIZE 14
char* button1File[ARRAYSIZE] = { "clip01.mp3", "clip02.mp3", "clip03.mp3", "clip04.mp3", "clip05.mp3", "clip06.mp3", "clip07.mp3", "clip08.mp3", "clip09.mp3", "clip10.mp3", "clip11.mp3", "clip12.mp3", "clip13.mp3", "clip14.mp3" };
int button1Used[ARRAYSIZE] ={0,0,0,0,0,0,0,0,0,0,0,0,0,0};

// create shield-example object!
Adafruit_VS1053_FilePlayer musicPlayer =
  Adafruit_VS1053_FilePlayer(SHIELD_RESET, SHIELD_CS, SHIELD_DCS, DREQ, CARDCS);
 
void setup()
{
  // initialize music maker             
  (! musicPlayer.begin());
  SD.begin(CARDCS);

  // set volume in left channel, right channel
  musicPlayer.setVolume(15,15);
  musicPlayer.useInterrupt(VS1053_FILEPLAYER_PIN_INT);

  // initialize relay
  digitalWrite(RELAY1,HIGH); 
  pinMode(RELAY1, OUTPUT); 
 
  // initialize serial monitor
  Serial.begin(9600);
  Serial.println("Start ");
}

void DoFilePlay(char* fileArr[],int usedArr[])
{
  // int as bool that will be 1 if all are used 0 if not.
  int allUsed = 1;

  // variable int for the new random number.
  int newRand = 0;

  // get new, unused, Random Number
  do
  {
    newRand = random (1,15)-1;
  }   while (usedArr[newRand] == 1);

  // set current file as used.
  usedArr[newRand] = 1;
               
  // check to see if all files have been used.
  for(int i=0;allUsed==1 && i<ARRAYSIZE;i++)
  {
    if(usedArr[i] == 0)
    {
      allUsed = 0;
    }
  }
               
  // if all files have been used, reset the files to have not been used.
  if(allUsed == 1)
  {
    for(int i=0;i<ARRAYSIZE;i++)
    {
      usedArr[i] = 0;
    }
  }
               
  // show the value in Serial Monitor
  Serial.println(newRand + 1);
 
  // turn relay on
  digitalWrite(RELAY1,LOW);
  Serial.println("Turn Relay On");

  // wait 2 seconds
  delay(2000);
               
  // play tune
  musicPlayer.playFullFile(fileArr[newRand]);
  Serial.println("Play Tune");
               
  // wait 2 seconds
  delay(2000);
               
  // turn relay Off
  digitalWrite(RELAY1,HIGH);
  Serial.println("Turn Relay Off");
}

void loop()
{
  // button 1 pressed
  if (musicPlayer.GPIO_digitalRead(2) == HIGH)
    {
      DoFilePlay(button1File,button1Used);
      return;
    }
}

cgrucelski
 
Posts: 2
Joined: Fri Jun 17, 2016 9:31 am

Re: Relay 'chatter' during MP3 audio playback

by adafruit_support_bill on Sat Jun 30, 2018 5:21 am

Code: Select all | TOGGLE FULL SIZE
// pin used for the external relay board
#define RELAY1  12

On and UNO, pin 12 is the MISO pin on the SPI bus. There will be activity on that pin whenever you are reading from the SD card.

adafruit_support_bill
 
Posts: 69809
Joined: Sat Feb 07, 2009 10:11 am

Re: Relay 'chatter' during MP3 audio playback

by cgrucelski on Sat Jun 30, 2018 11:28 am

I switched to pin 8 and no chatter.

I should've thought to try another pin.

Thanks so much!

cgrucelski
 
Posts: 2
Joined: Fri Jun 17, 2016 9:31 am

Re: Relay 'chatter' during MP3 audio playback

by adafruit_support_bill on Sat Jun 30, 2018 11:42 am

Good to hear it is working for you. Thanks for the follow-up.

adafruit_support_bill
 
Posts: 69809
Joined: Sat Feb 07, 2009 10:11 am

Please be positive and constructive with your questions and comments.