Arduino input and Output

This lesson will explore the use of the write and read function in an Arduino. We will go through the significant parts of the write function, their capability, and where they can be used. Arduino board has input/output pins that can be controlled to give the desired functionality to a particular project. It is through the input/output pins that one can control actuators and sensors to automate different items.

However, the Arduino write function can be used to control analog and digital pins. The choice of control will depend on the type of component controlled. For instance, we can turn the LED on and off at full brightness using the digitalWrite () function. However, to control the brightness of the LED, we must use the analogWrite () function. In this lesson, we will focus on the actualization of the write function to control different components. NOTE: To use the write function, the pin must be set up as an output.

Required Hardware

S.No.     Item                      Quantity

1              Arduino board          1

2              Breadboard              1

3              LED                          1

4              Resistor 220k           1

5              Jumper wires

6              Potentiometer           1

Procedure

Build the Circuit

Connect the components, as indicated above. The jumper wires should be used as the connectors. The schematic represents a close replica of the actual circuit board. However, the real components may be different from the actual ones. All that matters is the pins of the components are connected to the corresponding pin number as indicated in the image.

Code

Once your connection is fully secured, it is time to program the project to get the desired output. Now you can copy and paste the code on a blank sketch.

//START COPYING FROM HERE

//Lesson on Arduino write function

//https://novatech.co.ke/

//Rotate the shaft of the potentiometer, and you should see the luminance of the LED change.

//****Variable Declaration****

int potentiometerPin = A0;//the analog input pin attach to pin A0

int ledPin = 9;//the led attach to pin 9 of the arduino board

int inputValue = 0;//variable to store the value coming from the potentiometer

int outputValue = 0;//variable to store the output value

void setup()

{

Serial.begin(9600);//set the serial communication baudrate as 9600

pinMode(ledPin,OUTPUT);

pinMode(potentiometerPin,INPUT);

digitalWrite(ledPin,HIGH);//Turning the LED to full brighness using the digitalWrite fucntion

delay(1000);//Waiting for 1 second or 1000 miliseconds

digitalWrite(ledPin,LOW);//Turning off the LED using the digitalWrite function

}

void loop()

{

inputValue = analogRead(potentiometerPin);//read the value from the potentiometer

Serial.print("Input: "); //print "Input"

Serial.println(inputValue); //print inputValue

outputValue = map(inputValue, 0, 1023, 0, 255); //Convert from 0-1023 proportional to the number of a number of from 0 to 255

Serial.print("Output: "); //print "Output"

Serial.println(outputValue); //print outputValue

analogWrite(ledPin, outputValue); //turn the LED on depending on the output value

//Note the write function used to contriol the LED.

//the value of outputValue will depend on the position of the potentiometer

}

//*****END***//

Click on upload the code to send the code to the Arduino board. If “Dones Uploading” appears at the bottom of the window, the sketch is successfully uploaded.

Now you can spin the shaft of the potentiometer to see the change on the LED brightness. You can open the serial monitor to see the changes in the output values and the potentiometer values to visualize the changes in the program as it is executed.

The program obtains the real-time value of the potentiometer position. The position is then mapped to values between 0-255 that can be used to control the brightness of the LED. The map () function will be explained in the next lesson.

The digitalWrite function has two arguments, the pin number and the state. The general syntax is dogitalWrite (pin number, state). digitalWrite function only accepts two states, HIGH or LOW. The analogWrite function also has two arguments, the pin number and the value. The general syntax of the analogWrite is analogWrite (pin number, value). The value argument accepts numbers between 0 and 255. However, you are free to explore other numbers to see how the LED behaves.

Explore

Now you can try to modify the program so that you can control the speed of blinking the LED using the potentiometer value.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *