0% found this document useful (0 votes)
45 views5 pages

Activity 1 Micro

The document contains code examples for using an Arduino board with various sensors and actuators including a keypad and LCD, ultrasonic sensor and buzzer, joystick and servo motors, and a stepper motor. The code snippets show how to initialize and interface with these components to read sensor input and control outputs.

Uploaded by

Aldrin taduran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views5 pages

Activity 1 Micro

The document contains code examples for using an Arduino board with various sensors and actuators including a keypad and LCD, ultrasonic sensor and buzzer, joystick and servo motors, and a stepper motor. The code snippets show how to initialize and interface with these components to read sensor input and control outputs.

Uploaded by

Aldrin taduran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Activity 1 - Arduino -keypad-LCD

*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-keypad-lcd
*/

#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

const int ROW_NUM = 4; // four rows


const int COLUMN_NUM = 4; // four columns

char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3', 'A'},
{'4','5','6', 'B'},
{'7','8','9', 'C'},
{'*','0','#', 'D'}
};

byte pin_rows[ROW_NUM] = {9, 8, 7, 6}; // connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {5, 4, 3, 2}; // connect to the column pinouts of the keypad

Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );


LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows

int cursorColumn = 0;

void setup(){
lcd.init(); // initialize the lcd
lcd.backlight();
}

void loop(){
char key = keypad.getKey();

if (key) {
lcd.setCursor(cursorColumn, 0); // move cursor to (cursorColumn, 0)
lcd.print(key); // print key at (cursorColumn, 0)

cursorColumn++; // move cursor to next position


if(cursorColumn == 16) { // if reaching limit, clear LCD
lcd.clear();
cursorColumn = 0;
}
}
}
Arduino-Ultrasonics-Buzzer

*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-ultrasonic-sensor-piezo-buzzer
*/

// constants won't change


const int TRIG_PIN = 6; // Arduino pin connected to Ultrasonic Sensor's TRIG pin
const int ECHO_PIN = 7; // Arduino pin connected to Ultrasonic Sensor's ECHO pin
const int BUZZER_PIN = 3; // Arduino pin connected to Piezo Buzzer's pin
const int DISTANCE_THRESHOLD = 50; // centimeters

// variables will change:


float duration_us, distance_cm;

void setup() {
Serial.begin (9600); // initialize serial port
pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode
pinMode(BUZZER_PIN, OUTPUT); // set arduino pin to output mode
}

void loop() {
// generate 10-microsecond pulse to TRIG pin
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

// measure duration of pulse from ECHO pin


duration_us = pulseIn(ECHO_PIN, HIGH);
// calculate the distance
distance_cm = 0.017 * duration_us;

if(distance_cm < DISTANCE_THRESHOLD)


digitalWrite(BUZZER_PIN, HIGH); // turn on Piezo Buzzer
else
digitalWrite(BUZZER_PIN, LOW); // turn off Piezo Buzzer

// print the value to Serial Monitor


Serial.print("distance: ");
Serial.print(distance_cm);
Serial.println(" cm");

delay(500);
}
Arduino-servo motor-joystick

/*

* Created by ArduinoGetStarted.com

* This example code is in the public domain

* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-joystick-servo-motor

*/

#include <Servo.h>

#define VRX_PIN A0 // Arduino pin connected to VRX pin

#define VRY_PIN A1 // Arduino pin connected to VRY pin

#define SERVO_X_PIN 2 // Arduino pin connected to Servo motor 1

#define SERVO_Y_PIN 3 // Arduino pin connected to Servo motor 2

Servo xServo; // create servo object to control a servo 1

Servo yServo; // create servo object to control a servo 2


void setup() {

Serial.begin(9600) ;

xServo.attach(SERVO_X_PIN);

yServo.attach(SERVO_Y_PIN);

void loop() {

// read analog X and Y analog values

int xValue = analogRead(VRX_PIN);

int yValue = analogRead(VRY_PIN);

int xAngle = map(xValue, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180)

int yAngle = map(yValue, 0, 1023, 0, 180); // scale it to the servo's angle (0 to 180)

xServo.write(xAngle); // rotate servo motor 1

yServo.write(yAngle); // rotate servo motor 2

// print data to Serial Monitor on Arduino IDE

Serial.print("Joystick: ");

Serial.print(xValue);

Serial.print(", ");

Serial.print(yValue);

Serial.print(" => Servo Motor: ");

Serial.print(xAngle);

Serial.print("°, ");

Serial.print(yAngle);

Serial.println("°");

}
Arduino Stepper Motor

/*

* Created by ArduinoGetStarted.com

* This example code is in the public domain

* Tutorial page: https://arduinogetstarted.com/tutorials/arduino-controls-28byj-48-stepper-motor-using-uln2003-driver

*/

// Include the AccelStepper Library

#include <AccelStepper.h>

// define step constant

#define FULLSTEP 4

#define STEP_PER_REVOLUTION 2048 // this value is from datasheet

// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence

AccelStepper stepper(FULLSTEP, 11, 9, 10, 8);

void setup() {

Serial.begin(9600);

stepper.setMaxSpeed(1000.0); // set the maximum speed

stepper.setAcceleration(50.0); // set acceleration

stepper.setSpeed(200); // set initial speed

stepper.setCurrentPosition(0); // set position

stepper.moveTo(STEP_PER_REVOLUTION); // set target position: 64 steps <=> one revolution

void loop() {

// change direction once the motor reaches target position

if (stepper.distanceToGo() == 0)

stepper.moveTo(-stepper.currentPosition());

stepper.run(); // MUST be called in loop() function

Serial.print(F("Current Position: "));

Serial.println(stepper.currentPosition());

You might also like