Skip to content

8. Input/Output devices

For this week, I worked on analyzing the different inputs and outputs to and from the arduino nano microcontroller. As the microcontroller coded using Arduino IDE to analyze different inputs like temperature and light and link them with voltage values that give an indication of the level of the temperature inside the room for example. Which most likely can be transferred from voltage to temperature (if the input is temperature) through a specified procedure or defined transfer function if available.

Inputs

Inputs can be classified into two categories: digital and analog inputs. On the Arduino nano microcontroller, there are 8 analog input ports (from 0 to 7) and 14 digital input ports (from 2 to 13). By default, the Arduino nano board mesures from ground to 5 volts. The functions of these ports will be illustrated more within the examples of the input and output devices and how they function.

Digital Temperature Sensor (KY-028)

The Digital Temperature Sensor (KY-028) is a device used for Arduino to measure the temperature by direct interaction based on thermistor resistance. This module exhibis both digital and analog types which would be further explained later on. Plus, It has a potetiometer to adjust the sensitivity of the device by manipulating the threshold.

To begin with, a simple code to take the temperature measurements was constructed which would be briefly explained, followed by setting up the temperature sensor with the arduino nano microcontroller. The code is shown in the following picture:

Firstly, the digital and analog pins that are going to be used for the input of both the digital and the analog type were defined. Also, both the digital value and the analog value were defined to be understood by the code. Serial.begin command was used. The 9600 number represents the bits to be introduced in each repeated loop. For the loop, the analog value (the voltage reading representing the temperature) was shown by the analog read command which directs the reading to the specified pin to be read from and the serial printin which prints the computed value. A delay was put between each reading. The digital value were not shown for a specific reason which would be explained now. In this teperature sensor, the digital response is represented in whether the sensor is on or off. To elaborate more, the temperatue sensor has a range from -55 to 125 degree celsius and based on the measured value (whether it is in the range or not), the sensor would be on or off by a simple digital response. The analog response is within the next step after the temperature sensor is turned on by fulfilling the previous constrain, the measurement of the temperatue itself (i.e. the voltage reading from the temperatue measurement) is the representation of the analog response in the temperature sensor. The reason behind not including the digital value/ command is that because under the available circumcstances where the temperatue sensor is used, the temperature is always between the specified range and hence it is always on whenever connected to the arduino nano board via the bread board. Here a picture of the temperature code after some adjustment, where the (reading=) is put to illustrate that the value got is the voltage reading and the digital pin was changed.

After the setting up of the code, the temperature sensor and the Arduino nano were connected to run the code. The following is a picture of the temperatue sensor after being connected:

Which was based on this figure:

Here we can see four pins: A0, G, + and D0. A0 and D0 are connected to the analog and digital pins respectively in the arduino nano microcontroller, which were later put in the code. G represents ground which was conneceted to the GND (ground). The + was connected to the 5V (5 volts). The following picture shows the connections on the breadboard to the arduino nano:

The following pictures represent the measurements values and the response curve

The response curve was at when the sensor was just in contact with the atmoshpere. After putting my finger on the sensor which was hotter than the air present, the curve started to decline as shown:

Then when my hand was removed, the response curve gradually retuned to the original setpoint which is as follows:

This can give an indicator that, at higher temperatue measurements, the voltage reading decreases. This might mean that there is an inverse relationship between the resistance and the temperature reading as at higher temperatue the voltage is decreasing (which means that the resistance is also decreasing as it is directly proportional to the voltage). Meaning at higher temperature readings, the resistance to temperature flow for reading decreases explaining the given phenomena.

The follwoing picture shows the temperatue sensor and how it is connected.

.

Photoresistor (KY-018)

The photoresistor module (KY-018) is a light sensor mainly used to measure and quantify the light intensity of a substance or area by identifying the presence or absence of light. The photoresistor has only analog type interactions.

Like the temperature sensor, a simple code to use the photoresistor module was established in order to analyze the input signals to the photoresistor. Here, the established code is shown below:

As the photoresistor has only an analog type, there is only one sesonr pin which is defined at the start of the code to be A2. The setup was put to take 9600 bits each trial (loop) and the value was set to zero at the beginning of the process (which would change depending on the light input (light intensity) to the photoresistor). In the loop section, the (analogRead) command as previously mentioned was used to assign the pin in which the values would be analyzed and read from. The (serial.printin) command was utilized to print out the values corresponing to the volage values based on an input of light.

The photoresistor was set with the microcontroller in the breadboard as follows:

This was based on this illustrative diagram:

Unlike the temperature sensor, here we can only see three pins on the photoresistor as it does not have a digital type and hence the signal can be concluded to be only analog signal with one pin whereas the temperature sensor which has both types has two pins for each. The S denoting the signal was connected to the analog pin assigned in the code which is A2, the negative sign representing the Ground was connected to the GND and the middle representing the power line was connected to the 5 volts (5V) pin. The following picture shows a close insight on the connections between the photoresistor and the microcontroller:

A response figure is constructed and looks as follows:

This figure shows the response to an input of light. At the beginning, the photo resistor was exposed to the light of the room at a regular rate without any interferance, the response value corresponding to this input was around 500 as shown in the figure. Moreover, when the photoresistor was covered by my hand (i.e. the light was blocked from reaching the photoresistor), the response value dramatically increased to around 1000. Later on, the fluctuations present in the figure represent me opening and closing my hand, as when I open my hand light goes to the sensor and hence the curve declines and when i close my hand the curve goes up. Finally, when i fuully released the sensor, the curve declined to the initial set point. The same phenomena witnessed in the temperature sensor is seen in the photoresistor as well. As at higher light intensities, the voltage response decreasing which is due to the decrease in the resistance of the sensor. This can be attriuted to the fact that at higher light intensity (i.e. more light is precense), the resistance to light flow to the photoresistor is becoming less and vice versa; explaining the phenomena occuring.

These two pictures shows the datasheet of the values corresponding to the above response:

LED output from the microcontroller using the photoresistor

Changes to the code used to test the photoresistor were made in order to manipulate the LED light present in the arduino microcontroller based on the light intensity registered by the photoresistor. Mainly, an if statement was added in order to make the LED light turn on when the light intensity falls below a certain threshold. As previously established that the resistance increases as the light intensity decreases leading to an increase to the voltage response from the arduino program, the threshold used in the if statement was for the voltage output to be greater than or equal to 700. This code can be used for sensitive glass houses that requires continous supply of light with a minimum supply rate, as the microcontroller can notify the user when the light is below the required threshold to light up. Nevertheless, the follwoing picture shows the used code:

Plus, here is how the datasheet is after the adjustments made to the code:

Finally, below is shown how the microcontroller response to the low light supply by switching on the LED light.

Outputs:

Outputs can be due to both digital and analog inputs. For example, a light bulb can turn on due to recieving a value or it would turn on after exceeding a certain threshold.

Servomotor (Micro Servo S51)

The Servo controller is considered one of the few special output devices that work on the pulsive signal princible, sending pulsive signal to the shaft of the motor to move it based on the required degree. Mainly, this servo motor works on the servo control mechanism, where a Pulsive-Width Modulation (PWM) signal to the servo motor which consists of a sequence of repeating pulses that determine the position of the servo. The servo code and commands are very simple, but to understand them more, the servo position was linked to the temperature sensor previously used in order to further understand the working princible.

Firstly, the previously established temperature code was modified to test the response of the servo motor for any temperature variation. The code used is shown below which would be explained later:

It can be seen in the first picture that, the servo library is added to the arduino code through the #include , servo is also defined. The Servo is defined to read from the 9th pin as by the (servo.attach (9)). An if statement is used for the servo motor to move, if the temperature goes beyond a specific threshold. The servo motor is set to move 90 degrees by the command (servo.write(angle)). Finally, some delays were put in order to wait partially between each conducted measurement.

The following shows the way both the temperatue and the servo motor connection to the microcontroller in the breadboard:

Which was based on this:

As shown in the code, the first slot (represented by the yellow wire in the illustrative diagram) is connected to the digital pin 9 which is as needed is a PWM pin. The middle one is connected to the 5V and the last wire is connected to the ground. After connecting, my thumb was applied on the temperature sensor to increase the temperature and hence decrease the voltage readings, leading to achieving the if statement to move the servo motor.

The following videos illustrate the achieved outcome of the conducted procedure:

This is a view of the connections between the servomotor, temperature sensor, breadboard and the microcontroller:


Last update: August 26, 2021