Originally published by Dec 31, 2016
We’ll be using SparkFun Ambient Light Sensor TEMT6000 to detect if it’s Day or Night. It’s important to distinguish the two in order to determine when and how often we release the food. TEMT6000 light sensor has 3 pins: SIG; GND; VCC. Wiring this sensor to the Arduino Board is quite simple: the VCC to the 5V pin; GND to board’s GND pin and the SIG needs to be connected to an Analog Input. I’ve chosen the A0 pin. The output pin SIG works as a transistor, so the greater the light applied near the sensor, the higher the voltage is on the output of the pin.
The graph below shows a relationship between the current and illuminance perceived by the TEMT6000. Illuminance is a measure of total luminous flux (i.e. visible light emitted by a source, measured in lm) divided by an area in m². Generally, 1 illuminance (Ix) = 1 Im/m². The TEMT6000 recognizes typical human visible spectrum with light wavelengths in the range of 390–700 nm.
TEMT6000 Datasheet: https://www.sparkfun.com/datasheets/Sensors/Imaging/TEMT6000.pdf?_ga=1.236184897.1967866376.1482950454
Figure 1: Collector light current vs Illuminance / ©Sparkfun
Figure 2: Wire diagram of TEMT6000 light sensor and Arduino Uno
Figure 3: Connection between TEMT6000 and Arduino Uno
Because this sensor is connected on an analog pin, the maximum value is 1023 due to the 10 bit resolution of the analog-to-digital converter. So for example, when the sensor is subjected on the highest light from my phone flash the value read from the sensor was around 1023.
Figure 4: Arduino serial monitor showing the maximum values of the sensor
The code for the light sensor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
int lightSensor = 0; void setup() { Serial.begin(9600); } void loop() { int valueFromLightSensor = analogRead(lightSensor); Serial.println(valueFromLightSensor); delay(1000); } |
To measure the distance I’ve chosen an analog sensor (Sharp GP2Y0A21YK) because it has the best results when compared to other distance sensors. The working principle of reflection is as follows: a signal is emitted and when it finds an obstacle in the way, it sends back also a signal (a voltage value that varies depending on how near/far is the obstacle); this voltage needs to be converted into distance.
The position of GP2Y0A21YK will be above the food container, measuring the emptiness of the bottle (i.e. food level) before initiating the automatic feeder function. With this distance sensor, the system will detect if in the food container (or bottle) is full or empty. The ways this program will work:
Here the distance implies the value from the place you mounted the distance sensor to the bottom of the food container. Because the range of the sensor is 10-80 cm, the sensor needs to be above the top of the food container with 10cm in order to read the correct value of the distance.
How to determine the best-fit line for the sensor:
From Pololu.com
The relationship between the sensor’s output voltage and the inverse of the measured distance is approximately linear over the sensor’s usable range. You can use this plot to convert the sensor output voltage to an approximate distance by constructing a best-fit line that relates the inverse of the output voltage (V) to distance (cm). In its simplest form, the linearizing equation can be that the distance to the reflective object is approximately equal to a constant scale factor (~27 V*cm) divided by the sensor’s output voltage. Adding a constant distance offset and modifying the scale factor can improve the fit of this line.
Figure 5: The characteristic of the distance sensor / ©Pololu.com
From Phidgets.com
Based on “typical values” from Sharp, the formula to translate SensorValue into Distance (the formula is only valid for a SensorValue between 80 to 500) is:
Distance (cm) = 4800/(SensorValue – 20)
This sensor can find the distance to objects that present a very narrow edge such as a wall at a very sharp angle.
Note: The output of this sensor will vary from unit to unit, and based on the characteristics of the target (reflectance, size, direction of motion, object alignment).
Figure 6: Wire diagram of Sharp GP2Y0A21YK to Uno
Figure 7: Connection between Sharp distance sensor, TEMT6000, and Uno
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
int lightSensor = 0; int distanceSensor=1; void setup() { Serial.begin(9600); } void loop() { int valueFromLightSensor = analogRead(lightSensor); Serial.print("Light Value= "); Serial.print(valueFromLightSensor); Serial.println(""); Serial.print("Distance Value= "); int valueFromDistanceSensor = analogRead(distanceSensor); int distance= 4800/(valueFromDistanceSensor - 20); Serial.print(distance); delay(1000); } |
The time will be determined using the RTC DS1307 model. A real time clock is a system whose function is to determine real time. This circuit works on the base of a crystal oscillator with a frequency of 32.768 kHz. The principle is similar to the watches. An electronic oscillator function based on mechanical resonance of crystal vibration creates a precise frequency. This frequency is used for tracking date and time from the computer.
It’s a practical module because it has a battery that provides continuity even when the system is shut down.
Figure 8: Wire diagram for RTC module
Figure 9: Connection between the RTC, Sharp distance sensor, TEMT6000, and Uno
For an optimal usage, it is necessary to add 2 libraries for the module to work.
These 2 libraries are found on my https://github.com/todeilatiberia/AutomaticFeeder:
We will run a test code in to check the module. When we upload the program to Arduino board, the serial monitor will show the current date and hour. These 2 libraries have an example code for finding date and hour, called “SetTime”.
Finding SetTime:
Click on Arduino IDE → File → Examples → DS1307RTC → SetTime
Figure 10: Finding SetTime on Arduino IDE
In Figure 11, you will see that the module works correctly as the current date and time are displayed.
Figure 11: Current date and time displayed correctly
For our application, we will only measure hour. To do this we need to extract from RTC module the exact hour. This will be done with the simple function of the RTC called “setSyncProvider(RTC.get)”. After implementing this function, you will be able to see the hour in serial monitor along with values from distance sensor and light sensor.
The code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <Time.h> #include <Wire.h> #include <DS1307RTC.h> int lightSensor = 0; int distanceSensor=1; void setup() { Serial.begin(9600); setSyncProvider(RTC.get); } void loop() { int valueFromLightSensor = analogRead(lightSensor); Serial.print("Light Value= "); Serial.print(valueFromLightSensor); Serial.println(""); Serial.print("Distance Value= "); int valueFromDistanceSensor = analogRead(distanceSensor); int distance= 4800/(valueFromDistanceSensor - 20); Serial.println(distance); Serial.print("Hour= "); Serial.println(hour()); delay(1000); } |
Figure 12: Hour displayed
Once you’ve built your smart automatic pet feeder, explore more of our guides to discover more brilliant Arduino projects: