Analog input
In general, controllers are equipped with Analog to Digital Converters
or short ADC
. This internal devices converts voltage potencial into numeric value which can be further used by written program. This is also the case in Arduino UNO converter by the function analogRead(pin_number)
. In this case the voltage range $[0.0\ V .. +5.0\ V]$ is converted in to range of numbers $[0 .. 1024]$.
Tasks:
- Unmount robot’s bumper and all connections to the switch.
- Equip the robot with distance sensor according to video and scheme (see [@fig:distance_sen]).
{#fig:distance_sen}
- Try next [@lst:310_Analog_Input] and check the output of distance sensor in Serial monitor.
const int DIST_SEN_PIN = A0;
void setup()
{
pinMode(DIST_SEN_PIN, INPUT);
Serial.begin(9600);
}
void loop()
{
int adc_value = analogRead(DIST_SEN_PIN);
Serial.println(adc_value);
delay(1000);
}
: Analog Input. {#lst:310_Analog_Input}
- Convert the
analog_sensor_value
intoinput_voltage
and measure the input voltage potencial with volt-meter. The formula for conversion can be programmed as:
float input_voltage = 5.0/1024 * adc_value;
-
From the datasheet for the distance sensor try to code the function for measuring the distance in cm. According to documentation there is almost linear trend between output voltage and $distance^{-1}$. Thus we can get good result with [@eq:calc_disd].
\(distance^{-1}[cm] = 0.045 V_{out}\){#eq:calc_disd}
Next example can be your guide to code the function.
float getDistance_cm()
{
int adc_value = analogRead(DIST_SEN_PIN);
float input_voltage = 5.0/1024 * adc_value;
float distance = 1/(0.045 * input_voltage);
return distance;
}
Questions:
- What kind of values do you getting from the reading of the distance sensor with the function
analogRead(A0)
? - Find the reasonable value where you should stop the robot.
- Measure the voltage potencial of the sensor’s output.
Summary:
Analog to digital converter - ADC
ADC is an electronic sistem that converts analog signal (voltage) to a digitalized values. In our particular case the range of an analog voltage from 0V to 5V is converted to range of numbers from 0 to 1024.
Issues:
<++>
<++>