Go Down

Topic: Sketch not working when only battery powered (Read 444 times) previous topic - next topic


Hello everyone,

I'm a complete newbie at this but I made this code, which is supposed to detect 5 colors and play a sound from a micro SD card based on which color is detected.

Code: [Select]
 This reads a wave file from an SD card and plays it using the I2S interface to
 a MAX08357 I2S Amp Breakout board.

 * Arduino/Genuino Zero, MKRZero or MKR1000 board
 * SD breakout or shield connected
 * MAX08357:
   * GND connected GND
   * VIN connected 5V
   * LRC connected to pin 0 (Zero) or pin 3 (MKR1000, MKRZero)
   * BCLK connected to pin 1 (Zero) or pin 2 (MKR1000, MKRZero)
   * DIN connected to pin 9 (Zero) or pin A6 (MKR1000, MKRZero)

 created 15 November 2016
 by Sandeep Mistry

#include <SD.h>
#include <ArduinoSound.h>
#include "Wire.h"
#include "Adafruit_TCS34725.h"
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_1X);

//button logic
int inPin = 5;         // the number of the input pin

int reading;           // the current reading from the input pin
int previous;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

// filename of wave file to play
const char SHEEP[] = "SHEEP2.WAV";
const char HORSE[] = "HORSE2.WAV";
const char ROOSTER[] = "ROOSTER2.WAV";
const char DOG[] = "DOG2.WAV";
const char PIG[] = "PIG2.WAV";

// variable representing the Wave File
SDWaveFile waveFile;

void setup() {

  if (!SD.begin(SDCARD_SS_PIN)) {

  pinMode(inPin, INPUT);

void loop() {

 uint16_t clearcol, red, green, blue;
 float average, r, g, b;
 tcs.getRawData(&red, &green, &blue, &clearcol);
 average = (red+green+blue)/3;

 r = red/average;
 g = green/average;
 b = blue/average;

if (r > 1.2) {
  if (r > 1.75){
  waveFile = SDWaveFile(HORSE);
  }else if(r > 1.45){
  waveFile = SDWaveFile(PIG);
  waveFile = SDWaveFile(DOG);
 }else if ((r < 0.95) && (g > 1.2) && (b < 0.9)) {
  waveFile = SDWaveFile(SHEEP);
 else if ((r < 0.8) && (g < 1.2) && (b > 1.2)) {
  waveFile = SDWaveFile(ROOSTER);
  reading = digitalRead(inPin);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, remember
  // the time
  if (reading == LOW && previous == HIGH && millis() - time > debounce) {

   time = millis();

  previous = reading;


It works when I have the MKRZero connected by USB but not when I use a 3.7V 2000mAh battery I got from Micro Center. The color sensor I'm using is the Adafruit TCS34725. I used some sketch I found online and it said the voltage my battery supplied was 4.3 V at full charge.

Anyone know what might be preventing my code from working on battery power?



How are you powering the TCS34725?

In battery mode the 5V pin won't be supplied by the MKR.

Go Up