Inputs & Outputs
Introduction:
In this tutorial, we will learn how to read sensor values from a QTI sensor and move a motor based on the sensor readings.
Before we start, let’s explain the difference between these two things; analog and digital. Analog is a mode where the amount of voltage being outputted can be changed, while digital is only on or off.
For example; when we use digitalWrite, we would write the port we are changing, then if it’s low or high (ex. digitalWrite (2,HIGH)), but when using analog, the amount of voltage outputted can be changed from the minimum value (0) to the maximum value(255). To use analog, we would use an analog pin in setup instead of a normal number (ex. pinMode(A1, OUTPUT)); analog pins are numbered A0-A5.
We would use analogWrite instead of digitalWrite when we want to output through the pin. The format we follow to use analogWrite is identical to the format we use for digitalWrite with one small difference:
analogWrite(pin_number, analog_value);
You specify the pin_number first and then the analog_value second.
The pin_number has to be a analog pin
The analog_value is a number between 0 to 255.
For example; we would write analogWrite (2, 255) to have the same outcome as the digital example, if we wanted the motor to go at roughly half speed, we would write analogWrite (2,128) instead. Using analog enables us to change the speed of the robot and opens up a variety of different uses.
First, we will declare variables to hold the pin numbers for the QTI sensor and the motor:
int sensor_pin = 2; //set the pin for the sensor
int motor_pin = A1; //set the pin for the motor
Next, we will write set the Serial monitor communication speed and the pin modes inside of the setup() function:
void setup() {
Serial.begin(9600); //set how fast the arduino should communicate with the computer. This is
used to determine how fast "Serial.println()" will output to the Serial Monitor
pinMode(sensor_pin, INPUT); //set sensor as input
pinMode(motor_pin, OUTPUT); //set motor as output
}
After finishing the setup() function, we will move onto the loop() function. Each time the loop() function runs, we want to read the value coming from the QTI sensor by using digitalRead() on the sensor_pin. We also want to print out the value of the reading to the Serial Monitor so that we can check the values ourselves to make sure the readings are correct.
void loop() {
int sensor_value = digitalRead(sensor_pin); //retrieve the value detected by the sensor
Serial.println(sensor_value); //print out the values on the Serial Monitor (so you can check them yourself)
After getting the value of the sensor, we check if the sensor value reading; if the reading is 1, then the sensor is detecting a low amount of light. If the reading is 0, then the sensor is detecting a high amount of light. In this case, we want to turn on the motor when the sensor detects low amounts of light:
void loop() {
int sensor_value = digitalRead(sensor_pin); //retrieve the value detected by the sensor
Serial.println(sensor_value); //print out the values on the Serial Monitor (so you can check them yourself)
if(sensor_value == 1){ //if the sensor detects low amounts of light
analogWrite(motor_pin, 255); //turn the motor on
}
else{
analogWrite(motor_pin, 0); //turn the motor off
}
}
Since digitalRead() returns either 1 or 0, we check to see if the returned value is equal to 1; if it is 1, then we know that the sensor is detecting low amounts of light.
This program should now turn on the motor when the QTI sensor detects a low amount of light; the motor should turn off when the QTI sensor detects high amounts of light.