Tuesday, February 7, 2017

Voltage Divider with Four Photoresistors and Servo Motor Connections


This past week I worked on writing code that would read in values from four different photoresistors and print them on the serial monitor of the Arduino. To do this, I made the circuit shown in figures 1 and 2. This circuit includes four photoresistors set up in a voltage divider and the output of each voltage divider is connected to Analog pins 0-3 on the Arduino.


Figure 1: Schematic of voltage divider circuit with four photoresistors and four 10K ohm resistors.
 
Figure 2: Voltage divider circuit with four photoresistors and four 10K ohm resistors.


The C code that reads the output values from each of the four photoresistors and prints the one that is exposed to the most light (Greatest output value) is included below:

 ___________________________________________________________________________________
 
void setup() {
  Serial.begin(9600);  //Begin serial communication
}

void loop() {

// reads the input on analog pins 0-3
  int sensorValue0 = analogRead(A0);
  int sensorValue1 = analogRead(A1);
  int sensorValue2 = analogRead(A2);
  int sensorValue3 = analogRead(A3);

// prints the headings of the columns
  Serial.println();
  Serial.print("Sensor 1");
  Serial.print("\t");   
  Serial.print("Sensor 2");
  Serial.print("\t");
  Serial.print("Sensor 3");
  Serial.print("\t");
  Serial.print("Sensor 4");
  Serial.print("\t");
  Serial.println("The Sensor with most light");

//prints the values from each of the four voltage dividers     
  Serial.print(sensorValue0);
  Serial.print("\t");
  Serial.print("\t"); 
  Serial.print(sensorValue1);
  Serial.print("\t");
  Serial.print("\t"); 
  Serial.print(sensorValue2);
  Serial.print("\t");
  Serial.print("\t"); 
  Serial.print(sensorValue3);
  Serial.print("\t");
  Serial.print("\t");

 
/*Finds the photoresistor that is exposed to the most amount
  of light which is the highest output value and prints the
  the number of the photoresistor
*/
  int greatestValue;
  if (sensorValue0 > sensorValue1)
    if (sensorValue0 > sensorValue2)
      if (sensorValue0 > sensorValue3)
        //Serial.print("Sensor 1");
        greatestValue = 1;
  if (sensorValue1 > sensorValue0)
    if (sensorValue1 > sensorValue2)
      if (sensorValue1 > sensorValue3)
        //Serial.print("Sensor 2");
        greatestValue = 2;  
  if (sensorValue2 > sensorValue0)
    if (sensorValue2 > sensorValue1)
      if (sensorValue2 > sensorValue3)
        //Serial.print("Sensor 3");
        greatestValue = 3; 
  if (sensorValue3 > sensorValue0)
    if (sensorValue3 > sensorValue1)
      if (sensorValue3 > sensorValue2)
        //Serial.print("Sensor 4");
        greatestValue = 4;
  Serial.print(greatestValue);  
  delay(1000);       
}

___________________________________________________________________________________

 
Example of the output on the Serial Monitor is shown in the Figure below.

 
Figure 3: Data that is displayed on the serial monitor of the Arduino IDE.
I am in the process of figuring out exactly what these numbers mean. The relationship between these output values and the amount of light is that the more the sensor is exposed to light, the greater the output value. This is opposite from the resistance because the more the sensor is exposed to light, the lower the resistance.


The servo motor came in this week and I started to play with the motor to see how it works. The following link was very helpful with understanding how to connect the servo motor to the Arduino and the figures below show the proper connections(Figures 4 and 5): https://www.arduino.cc/en/Tutorial/Sweep

The link includes example code that is also found in the Arduino Examples that rotates the motor 180 degrees in one direction and then 180 degrees in the other direction.


Figure 4 shows the proper connections between the Arduino and the servo motor.

Figure5 shows the schematic of the proper connections between the Arduino and the servo motor
 

When running this example code, I discovered that the servo motor only turns 180 degrees (90 degrees in each direction). This is a problem because if the tiny servo motor turns 180 degrees, this would only turn the Lazy Susan a few degrees. I will have to come up with a design that works like a gear system so that I am able to turn the Lazy Susan 180 degrees.

This coming week, I will figure out a design to turn the Lazy Susan more than just a few degrees. I will also modify the code so that when photoresistor 1 or 2 is receiving the most light, then the motor turns one direction and when photoresistor 3 or 4 is receiving the most light, the motor turns in the opposite direction.

1 comment: