I'm working on a project where I have to use SG90 with TCS230. I have powered TCS230 with Arduino's 5V volt pin and I have powered SG 90 with a 9V 2A adapter. Both grounds were connected. In this project, if TCS230 detected value between 1 to 10, then the servo will run. But here, though my TCS230 caught value, my servo did not run and stayed still. Is the problem with my power supply or else?
#include<Servo.h>
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8
// Variables for Color Pulse Width Measurements
Servo myservo;//set servo name, here servo name is "myservo"
int redPW = 0;
int greenPW = 0;
int bluePW = 0;
void setup() {
// Set S0 - S3 as outputs
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
// Set Sensor output as input
pinMode(sensorOut, INPUT);
// Set Pulse Width scaling to 20%
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
// Setup Serial Monitor
Serial.begin(9600);
//set servo pin
myservo.attach(9);
}
void loop() {
// Read Red Pulse Width
redPW = getRedPW();
// Delay to stabilize sensor
delay(2000);
// Read Green Pulse Width
greenPW = getGreenPW();
// Delay to stabilize sensor
delay(1000);
// Read Blue Pulse Width
bluePW = getBluePW();
// Delay to stabilize sensor
delay(1000);
// Print output to Serial Monitor
Serial.print("Red PW = ");
Serial.print(redPW);
Serial.print(" - Green PW = ");
Serial.print(greenPW);
Serial.print(" - Blue PW = ");
Serial.println(bluePW);
if (redPW >= 1 && redPW <= 10)
{
Serial.print("Red");
myservo.write(45);
delay(500);
myservo.write(90);
delay(500);
}
else
{
Serial.print("others");
}
}
// Function to read Red Pulse Widths
int getRedPW() {
// Set sensor to read Red only
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
// Define integer to represent Pulse Width
int PW;
// Read the output Pulse Width
PW = pulseIn(sensorOut, LOW);
// Return the value
return PW;
}
// Function to read Green Pulse Widths
int getGreenPW() {
// Set sensor to read Green only
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
// Define integer to represent Pulse Width
int PW;
// Read the output Pulse Width
PW = pulseIn(sensorOut, LOW);
// Return the value
return PW;
}
// Function to read Blue Pulse Widths
int getBluePW() {
// Set sensor to read Blue only
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
// Define integer to represent Pulse Width
int PW;
// Read the output Pulse Width
PW = pulseIn(sensorOut, LOW);
// Return the value
return PW;
}
(I've used a 9V 2A adapter which gives almost 800 mA, but there is no figure for an adapter in fritzing so to create a diagram I've inserted a 9V battery figure)

