Digital and analog sensors have different strengths and different weaknesses that you need to be aware of, especially when you are trying to get your cart to do something. Analog sensors are great at sensing distances that are further away, but they become very unreliable when it is within 10 cm from the object. On the other hand, digital sensors can't sense anything that is further than 10cm, but are reliable within 10cm.
Using this, you need to make a decision on which to use based on your task. For example, if you are trying to get a cart to drive through a tight space, a digital sensor would be be best because it will sense something within this small area. However, if you are trying to get your cart to drive in a path that allows a distance between the obstacles and the cart, it would best to use analog ones.
The digital sensors detect if there is anything within 10 centimeters from the sensor. If there is something there, the light on the sensor will turn red, and if not, the light will not illuminate. When using the code given below, if the sensor is detecting something, the reading will be printed as '0'. If the sensor is not detecting anything, the reading will be printed as '1'. Unlike analog sensors, '0' and '1' are the only two ouputs of the digital sensors.
Digital sensors only send a fully on current or no current at all through their signal cable. Using them with a RoboPi hat is simple:
sensor_pin = 17
RPL.pinMode(sensor_pin,RPL.INPUT)
reading = RPL.digitalRead(sensor_pin)
Analog sensors send a current bases off of the distance that is read. When using an analog sensor with a RoboPi hat plug the sensor into a ADC pin. Pins Servo 0 - 7 and ADC pins share bandwidth; This means that a device can not be plugged into a Servo 0 - 7 pin and an ADC pin label with the same number. Example: Servo 5 and ADC 5 can not be used at the same time. Once the distance is less than 10cm the sensor is no longer reliable.
Python Example:
sensor_pin = 2
print RPL.analogRead(2)
Analog Sensors - Plug on the far side near the digital sensors on the side with the green light
Digital Sensors: On the same side as the green light between the analog pins and green light
Servo Motors: Right next to the red light
*Picture is wrong –– analog and digital sensor labels are opposite
Analog sensors provide a number on a scale of 0- []. This numerical scale is only useful if you know how to use it. It is easy to get a numerical reading of how far away from a specific wall or object the cart is, but it is important to then convert that information into something actually useful. To relate the numerical value to a certain code that allows the cart to stop, turn or drive, you must use a “while” or “if” statement to connect the analog reading to control the cart. For example:
while RPL.analogRead(7) < 629:
RPL.servoWrite(1,1400)
RPL.servoWrite(0,1600)
This code makes the cart drive straight forward when the reading from the analog sensor is less than “629”.
In order to determine the value you want to use to stop your cart, you will first need to determine how far away from a wall you want your cart to stop. Then you will need to test what the analog read is when your cart is this distance from the wall. A wall or a large material that will not move, such as a large piece of cardboard or plastic, will be the most reliable for getting an accurate reading (Make sure you don’t attempt to make the analog sensor sense a notebook or a hand!). Then place your cart the distance from the wall that you want the cart to stop (must be above 10 cm away or readings will be unreliable). Run a code similar to:
RPL.pinMode(7, RPL.INPUT)
values = []
for i in range(0,100):
values.append(RPL.analogRead(7))
print sum(values) / 100
This code will allow you to see the average reading the analog senor picks up at this distance.
How to Get Data Output on RaspberryPi:
Thats literally it. The potentiometers will output numbers based on where the dial is.
One thing to know is that the Analog pins are treated different then the other pins. What that means is that the analog pins are 1 - 8 vs something like sensor pins which have only 8 pins but the 8th pin is pin 23.
TSC34725 Color Sensor documentation
VIN = 5 volt input
GND = Ground
SDA = Signal
SCL = Signal
LED (Optional) = Turn the light on/off using RPL.servoWrite(pin,0) change 0 to 1 to turn LED off
---
Use this pinout for the two signal pins--MAKE SURE to connect SDA and SCL pins to the correct place on the GPIO pins (Top right of RoboPi)
The pins must be in the correct GPIO slots for I2C to recognize the color sensorThe ground can be wired to any of the pins on the OUTSIDE of the RoboPi pins
The 5v can be wired to any of the pins in the MIDDLE of the RoboPi pins
To use the color sensor, you must enable I2C on the Raspberry Pi
login into pi by using the command su pi
and type in the administrator password
Next, use sudo raspi-config
to enter the configuration tool
Locate 'Interfacing Options' and enable 'I2C'