Skip to content

7. Input & Output device

what i have learned :

What is the difference between an input and output device? An input device sends information to a computer system for processing, and an output device reproduces or displays the results of that processing. Input devices only allow for input of data to a computer and output devices only receive the output of data from another device.

For example, as shown in the top half of the image, a keyboard sends electrical signals, which are received as input. Those signals are then interpreted by the computer and displayed, or output, on the monitor as text or images. In the lower half of the image, the computer sends, or outputs, data to a printer. Then, that data is printed onto a piece of paper, which is also considered output.

Components Required You will need the following components −

1x Arduino UNO board 1x PN2222 Transistor 1x Small 6V DC Motor 1x 1N4001 diode 1x 270 Ω Resisto

Precautions: Take the following precautions while making the connections.

First, make sure that the transistor is connected in the right way. The flat side of the transistor should face the Arduino board as shown in the arrangement.

Second, the striped end of the diode should be towards the +5V power line according to the arrangement shown in the image.

the code:

Spin ControlArduino Code int motorPin = 3;

void setup() {

}

void loop() { digitalWrite(motorPin, HIGH); }

Arduino Code int motorPin = 9;

void setup() { pinMode(motorPin, OUTPUT); Serial.begin(9600); while (! Serial); Serial.println(“Speed 0 to 255”); }

void loop() { if (Serial.available()) { int speed = Serial.parseInt(); if (speed >= 0 && speed <= 255) { analogWrite(motorPin, speed); } } }

In the ‘loop’ function, the command ‘Serial.parseInt’ is used to read the number entered as text in the Serial Monitor and convert it into an ‘int’. You can type any number here. The ‘if’ statement in the next line simply does an analog write with this number, if the number is between 0 and 255.

Result The DC motor will spin with different speeds according to the value (0 to 250) received via the serial port.


Last update: August 16, 2022