6. Input & Output device¶
Throughout this week, my focus was on the microcontroller and its integration with input and output devices. The process involved reading data from a sensor, transmitting it through the microcontroller, and subsequently utilizing the input data to generate a corresponding output signal for the output device.
input¶
It is required to use an input component and see its output in the serial monitor of Arduino mkr1010
Joystick
The joystick is a common input device used in various electronic applications. It typically consists of a movable lever or knob that can be pushed or tilted in different directions. The movement of the joystick generates analog signals that can be used to control variables such as position, speed, or direction
This is the code:
const int joystickXPin = A0; // Analog input pin for X-axis
const int joystickYPin = A1; // Analog input pin for Y-axis
const int buttonPin = 2; // Digital input pin for button
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Set button pin as input with internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// Read analog values from X and Y axis
int xValue = analogRead(joystickXPin);
int yValue = analogRead(joystickYPin);
// Read button state
int buttonState = digitalRead(buttonPin);
// Print values to serial monitor
Serial.print("X-axis: ");
Serial.print(xValue);
Serial.print("\tY-axis: ");
Serial.print(yValue);
Serial.print("\tButton: ");
Serial.println(buttonState);
delay(100); // Adjust delay as needed
}
Here is the output
Output¶
Micro Servo Motor
A motor type that is a rotation movement output device powered by a DC Source, either from an external supply or by a controller, small and lightweight with high power output, this motor is accurate, can define starting and ending angles of rotation with certain speed, it was connected to pin 9 of digital for output, it does operate at 4.8-6V, and move according to the electrical signals provided to it by the microcontroller from the uploaded program code and turns it into kinetic energy in the form of rotational movement, the code can adjust the speed of the micro servo motors rotation and when it starts and stops.
I used the MKR1010 board in conjunction with servo motors as output devices. These servo motors are self-contained electrical devices known for their high efficiency and precise rotation of machine parts. Unlike regular motors, they have the ability to move their output shaft to specific angles, positions, and velocities. Operating within a voltage range of 4 to 7.2 volts, they effectively convert received signals into corresponding movements.
low speed code
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(5); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(45); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(45); // waits 15 ms for the servo to reach the position
}
}
High speed code
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(5); // attaches the servo on pin 9 to the servo object
}
void loop() {
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 15 ms for the servo to reach the position
}
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(1); // waits 15 ms for the servo to reach the position
}
}
Connection between servo and joystick¶
The circuit diagram
#include <Servo.h>
// Define the pins for the joystick
const int joystickXPin = A0;
const int joystickYPin = A1;
// Define the pins for the servo motor
const int servoXPin = 5;
const int servoYPin = 5;
// Create servo objects
Servo servoX;
Servo servoY;
// Variables to store joystick values
int joystickXValue = 0;
int joystickYValue = 0;
// Variables to map joystick values to servo angles
int servoAngleX = 0;
int servoAngleY = 0;
void setup() {
// Attach servos to the servo pins
servoX.attach(servoXPin);
servoY.attach(servoYPin);
// Set initial positions of the servos
servoX.write(90);
servoY.write(90);
// Serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Read the joystick values
joystickXValue = analogRead(joystickXPin);
joystickYValue = analogRead(joystickYPin);
// Map the joystick values to servo angles
servoAngleX = map(joystickXValue, 0, 1023, 0, 180);
servoAngleY = map(joystickYValue, 0, 1023, 0, 180);
// Control the direction of the servo motors based on joystick values
if (joystickXValue < 500) {
servoX.write(servoAngleX);
} else if (joystickXValue > 550) {
servoX.write(180 - servoAngleX);
}
if (joystickYValue < 500) {
servoY.write(servoAngleY);
} else if (joystickYValue > 550) {
servoY.write(180 - servoAngleY);
}
// Print the joystick and servo values
Serial.print("Joystick X: ");
Serial.print(joystickXValue);
Serial.print(" Joystick Y: ");
Serial.print(joystickYValue);
Serial.print(" Servo Angle X: ");
Serial.print(servoAngleX);
Serial.print(" Servo Angle Y: ");
Serial.println(servoAngleY);
// Add some delay for stability
delay(10);
}