Arduino Nano is not working when using external powerbank, can't find solution

Hi,

I’m really having trouble when I unplug my Arduino Nano (V3) from my computer (plugged in, it works fine). My project is a colour sensor (TCS3200) to sound (8ohm speaker) and LED output.

I have tried connecting it to a wall socket with the mini B cable as well as a variety of power banks (used for charging phones). The problem is that the various colours are no longer read by the sensor when I use the power banks as opposed to connecting it to a desktop or laptop.

Could someone tell me if it is the voltage supplied by the computers that is different? I am using a 5V power source though!

Here is the code:

#include <SPI.h>
#include <SD.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10

File root;
TMRpcm tmrpcm;

#define S0 14
#define S1 16
#define S2 17
#define S3 7
#define sensorOut 8

//int redPin = 9;
int redPin = 6;
int greenPin = 3;
int bluePin = 5;

int frequency_red = 0;
int frequency_grn = 0;
int frequency_ble = 0;

void setup() {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  // Setting frequency-scaling to 20%
  digitalWrite(S0, LOW);
  digitalWrite(S1, HIGH);
  tmrpcm.speakerPin = 9;
  //tmrpcm.setVolume(7);
  
  Serial.begin(9600);
  
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

  root = SD.open("/");

  printDirectory(root, 0);

  Serial.println("done!");
  
}

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 loop() {
  // Setting red filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);
  // Reading the output frequency
  frequency_red = pulseIn(sensorOut, LOW);
  // Printing the value on the serial monitor
  Serial.print("R= ");//printing name
  Serial.print(frequency_red);//printing RED color frequency
  Serial.print("  ");
  delay(100);

  // Setting Green filtered photodiodes to be read
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  // Reading the output frequency
  frequency_grn = pulseIn(sensorOut, LOW);
  // Printing the value on the serial monitor
  Serial.print("G= ");//printing name
  Serial.print(frequency_grn);//printing RED color frequency
  Serial.print("  ");
  delay(100);

  // Setting Blue filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  // Reading the output frequency
  frequency_ble = pulseIn(sensorOut, LOW);
  // Printing the value on the serial monitor
  Serial.print("B= ");//printing name
  Serial.print(frequency_ble);//printing RED color frequency
  Serial.println("  ");
  delay(100);
  
  if ((frequency_red > 925 && frequency_red < 940) && (frequency_grn > 1080 && frequency_grn < 1100) && (frequency_ble > 830 && frequency_ble < 845 ))
  {
    Serial.println("red");
    setColor(255, 0, 0); // Red
    tmrpcm.play("1.wav"); 
  }
    if ((frequency_red > 1000 && frequency_red < 1081) && (frequency_grn > 1170 && frequency_grn < 1205) && (frequency_ble > 898 && frequency_ble < 920 ))
  {
    Serial.println("red");
    setColor(255, 0, 0); // Red
    tmrpcm.play("1.wav"); 
  }
    if ((frequency_red > 940 && frequency_red < 960) && (frequency_grn > 1150 && frequency_grn < 1180) && (frequency_ble > 880 && frequency_ble < 910))
  {
    Serial.println("red orange");
    setColor(255, 69, 0); // Red Orange 
    tmrpcm.play("2.wav"); 
  }
    if ((frequency_red > 912 && frequency_red < 944) && (frequency_grn > 1080 && frequency_grn < 1107) && (frequency_ble > 870 && frequency_ble < 897))
  {
    Serial.println("orange");
    setColor(255, 140, 0); // Orange
    tmrpcm.play("3.wav"); 
  }
    if ((frequency_red > 940 && frequency_red < 944) && (frequency_grn > 1090 && frequency_grn < 1110) && (frequency_ble > 885 && frequency_ble < 907))
  {
    Serial.println("yellow orange");
    setColor(255, 215, 0); // Yellow Orange
    tmrpcm.play("4.wav"); 
  }
    if ((frequency_red > 870 && frequency_red < 917) && (frequency_grn > 959 && frequency_grn < 1005) && (frequency_ble > 857 && frequency_ble < 907))
  {
    Serial.println("yellow");
    setColor(190, 255, 0); // Yellow
    tmrpcm.play("5.wav"); 
  }
      if ((frequency_red > 1100 && frequency_red < 1153) && (frequency_grn > 1085 && frequency_grn < 1135) && (frequency_ble > 889 && frequency_ble < 942))
  {
    Serial.println("yellow green");
    setColor(0, 255, 10); // Yellow Green
    tmrpcm.play("6.wav"); 
  }
  if ((frequency_red > 1158 && frequency_red < 1230) && (frequency_grn > 1146 && frequency_grn < 1220) && (frequency_ble > 910 && frequency_ble < 972))
  {
    Serial.println("green");
    setColor(125, 230, 0); // Green
    tmrpcm.play("7.wav"); 
  }
        if ((frequency_red > 1140 && frequency_red < 1210) && (frequency_grn > 1095 && frequency_grn < 1165) && (frequency_ble > 835 && frequency_ble < 895))
  {
    Serial.println("blue green");
    setColor(0, 255, 255); // Green Blue
    tmrpcm.play("8.wav"); 
  }
  if ((frequency_red > 1170 && frequency_red < 1240) && (frequency_grn > 1164 && frequency_grn < 1233) && (frequency_ble > 835 && frequency_ble < 899))
  {
    Serial.println("blue");
    setColor(0, 0, 255); // Blue
    tmrpcm.play("9.wav");
  }
    if ((frequency_red > 1153 && frequency_red < 1226) && (frequency_grn > 1218 && frequency_grn < 1274) && (frequency_ble > 883 && frequency_ble < 943))
  {
    Serial.println("blue violet");
    setColor(133, 21, 255); // Blue Violet
    tmrpcm.play("10.wav");
  }
    if ((frequency_red > 1095 && frequency_red < 1140) && (frequency_grn > 1215 && frequency_grn < 1245) && (frequency_ble > 869 && frequency_ble < 920))
  {
    Serial.println("violet");
    setColor(150, 50, 180); // Violet
    tmrpcm.play("11.wav");
  }
    if ((frequency_red > 1060 && frequency_red < 1065) && (frequency_grn > 1179 && frequency_grn < 1233) && (frequency_ble > 868 && frequency_ble < 915))
  {
    Serial.println("red violet");
    setColor(255, 21, 133); // Red Violet
    tmrpcm.play("12.wav");
  }

}

void setColor(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}

Please help!

If you aren't actually using the serial output I'd remark out all those lines for starters...

Thanks, I did that, but to no avail :frowning: any other suggestions?

#include <SPI.h>
#include <SD.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10

File root;
TMRpcm tmrpcm;

#define S0 14
#define S1 16
#define S2 17
#define S3 7
#define sensorOut 8

//int redPin = 9;
int redPin = 6;
int greenPin = 3;
int bluePin = 5;

int frequency_red = 0;
int frequency_grn = 0;
int frequency_ble = 0;

void setup() {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(sensorOut, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  // Setting frequency-scaling to 20%
  digitalWrite(S0, LOW);
  digitalWrite(S1, HIGH);
  tmrpcm.speakerPin = 9;
  //tmrpcm.setVolume(7);


 // Serial.begin(9600);
 // while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
//  }
  

//  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
//    Serial.println("initialization failed!");
    return;
  }
//  Serial.println("initialization done.");

  root = SD.open("/");

//  printDirectory(root, 0);

//  Serial.println("done!");
  

//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 loop() {
  // Setting red filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);
  // Reading the output frequency
  frequency_red = pulseIn(sensorOut, LOW);
  // Printing the value on the serial monitor
//  Serial.print("R= ");//printing name
//  Serial.print(frequency_red);//printing RED color frequency
//  Serial.print("  ");
  delay(100);

  // Setting Green filtered photodiodes to be read
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);
  // Reading the output frequency
  frequency_grn = pulseIn(sensorOut, LOW);
  // Printing the value on the serial monitor
//  Serial.print("G= ");//printing name
//  Serial.print(frequency_grn);//printing RED color frequency
//  Serial.print("  ");
  delay(100);
  // Setting Blue filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);
  // Reading the output frequency
  frequency_ble = pulseIn(sensorOut, LOW);
  // Printing the value on the serial monitor
//  Serial.print("B= ");//printing name
//  Serial.print(frequency_ble);//printing RED color frequency
//  Serial.println("  ");
  delay(100);
  
  if ((frequency_red > 900 && frequency_red < 971) && (frequency_grn > 1080 && frequency_grn < 1142) && (frequency_ble > 780 && frequency_ble < 879 ))
  {
//    Serial.println("red");
    setColor(255, 0, 0); // Red
    tmrpcm.play("1.wav"); 
  }
    if ((frequency_red > 1015 && frequency_red < 1081) && (frequency_grn > 1194 && frequency_grn < 1275) && (frequency_ble > 910 && frequency_ble < 970 ))
  {
//    Serial.println("red");
    setColor(255, 0, 0); // Red
    tmrpcm.play("1.wav"); 
  }
    if ((frequency_red > 950 && frequency_red < 1005) && (frequency_grn > 1170 && frequency_grn < 1222) && (frequency_ble > 894 && frequency_ble < 950))
  {
//    Serial.println("red orange");
    setColor(255, 69, 0); // Red Orange 
    tmrpcm.play("2.wav"); 
  }
    if ((frequency_red > 918 && frequency_red < 972) && (frequency_grn > 1098 && frequency_grn < 1150) && (frequency_ble > 880 && frequency_ble < 930))
  {
//    Serial.println("orange");
    setColor(255, 140, 0); // Orange
    tmrpcm.play("3.wav"); 
  }
    if ((frequency_red > 896 && frequency_red < 944) && (frequency_grn > 1029 && frequency_grn < 1079) && (frequency_ble > 872 && frequency_ble < 923))
  {
//    Serial.println("yellow orange");
    setColor(255, 215, 0); // Yellow Orange
    tmrpcm.play("4.wav"); 
  }
if ((frequency_red > 870 && frequency_red < 917) && (frequency_grn > 959 && frequency_grn < 1005) && (frequency_ble > 857 && frequency_ble < 907))
  {
//    Serial.println("yellow");
    setColor(190, 255, 0); // Yellow
    tmrpcm.play("5.wav"); 
  }
      if ((frequency_red > 1100 && frequency_red < 1153) && (frequency_grn > 1085 && frequency_grn < 1135) && (frequency_ble > 889 && frequency_ble < 942))
  {
//    Serial.println("yellow green");
    setColor(0, 255, 10); // Yellow Green
    tmrpcm.play("6.wav"); 
  }
  if ((frequency_red > 1158 && frequency_red < 1230) && (frequency_grn > 1146 && frequency_grn < 1220) && (frequency_ble > 910 && frequency_ble < 972))
  {
//    Serial.println("green");
    setColor(125, 230, 0); // Green
    tmrpcm.play("7.wav"); 
  }
        if ((frequency_red > 1140 && frequency_red < 1210) && (frequency_grn > 1095 && frequency_grn < 1165) && (frequency_ble > 835 && frequency_ble < 895))
  {
//    Serial.println("blue green");
    setColor(0, 255, 255); // Green Blue
    tmrpcm.play("8.wav"); 
  }
  if ((frequency_red > 1170 && frequency_red < 1240) && (frequency_grn > 1164 && frequency_grn < 1233) && (frequency_ble > 835 && frequency_ble < 899))
  {
//  Serial.println("blue");
    setColor(0, 0, 255); // Blue
    tmrpcm.play("9.wav");
  }
    if ((frequency_red > 1153 && frequency_red < 1226) && (frequency_grn > 1218 && frequency_grn < 1274) && (frequency_ble > 883 && frequency_ble < 943))
  {
//    Serial.println("blue violet");
    setColor(133, 21, 255); // Blue Violet
    tmrpcm.play("10.wav");
  }
    if ((frequency_red > 1095 && frequency_red < 1140) && (frequency_grn > 1215 && frequency_grn < 1245) && (frequency_ble > 869 && frequency_ble < 920))
  {
//    Serial.println("violet");
    setColor(150, 50, 180); // Violet
    tmrpcm.play("11.wav");
  }
    if ((frequency_red > 1013 && frequency_red < 1065) && (frequency_grn > 1179 && frequency_grn < 1233) && (frequency_ble > 868 && frequency_ble < 915))
  {
//    Serial.println("red violet");
    setColor(255, 21, 133); // Red Violet
    tmrpcm.play("12.wav");
  }

}

void setColor(int redValue, int greenValue, int blueValue) {
  analogWrite(redPin, redValue);
  analogWrite(greenPin, greenValue);
  analogWrite(bluePin, blueValue);
}

Here is my schematic diagram-- the 5V and GNDs are being shared amongst more than 1 component. Could this be a factor?