3.Controlling a Servo Motor with a Keypad and Arduino

Controlling a Servo Motor with a Keypad and Arduino



Components Needed:

  1. Arduino board
  2. 4x4 or 4x3 Keypad
  3. Servo motor
  4. Jumper wires
  5. Breadboard (optional)

Connections:

  1. Keypad: Connect the keypad pins to digital pins on your Arduino (e.g., rows to pins 9, 8, 7, 6 and columns to pins 5, 4, 3, 2).
  2. Servo: Connect the servo motor:
    • Signal pin to a PWM digital pin on the Arduino (e.g., pin 10).
    • Power (VCC) to 5V.
    • Ground (GND) to GND.

Example Code:

C++ Code


#include <Keypad.h> #include <Servo.h> const byte ROWS = 4; // Number of rows const byte COLS = 4; // Number of columns char keys[ROWS][COLS] = { {'1', '2', '3', 'A'}, {'4', '5', '6', 'B'}, {'7', '8', '9', 'C'}, {'*', '0', '#', 'D'} }; byte rowPins[ROWS] = {9, 8, 7, 6}; // Connect to row pins of keypad byte colPins[COLS] = {5, 4, 3, 2}; // Connect to column pins of keypad Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); Servo myServo; // Create a Servo object int servoPin = 10; // Connect servo signal pin to pin 3 int angle = 0; // Servo position (0 to 180 degrees) void setup() { myServo.attach(servoPin); // Attach the servo to the pin myServo.write(angle); // Set initial servo position to 0 degrees Serial.begin(9600); } void loop() { char key = keypad.getKey(); if (key) { Serial.println(key); // Control servo based on key press switch (key) { case '1': angle = 0; // Set servo to 0 degrees break; case '2': angle = 90; // Set servo to 90 degrees break; case '3': angle = 180; // Set servo to 180 degrees break; case '4': angle += 10; // Increment servo angle by 10 degrees if (angle > 180) angle = 180; // Limit to 180 degrees break; case '5': angle -= 10; // Decrement servo angle by 10 degrees if (angle < 0) angle = 0; // Limit to 0 degrees break; default: break; } myServo.write(angle); // Move servo to the specified angle } }

Explanation:

  1. Libraries Import: The Keypad and Servo libraries are included for keypad and servo control.
  2. Define Rows and Columns: Set the number of rows and columns of the keypad.
  3. Key Mapping and Pin Assignments: Assign the keypad pins and map the keys.
  4. Servo Setup: Define the servo pin, create a Servo object, and attach it to the pin.
  5. Loop Logic:
    • Use keypad.getKey() to detect key presses.
    • Depending on the key pressed:
      • Keys '1', '2', and '3' set the servo to 0, 90, and 180 degrees, respectively.
      • Keys '4' and '5' adjust the servo angle incrementally by 10 degrees.

Tips:

  • Make sure to use an external power supply if your servo motor requires more current than the Arduino can provide.
  • You can modify the keys to control the servo in different ways, such as using other keys for more precision or adding more preset positions.

Comments

Popular posts from this blog

2.Controlling LEDs with a Keypad and Arduino

Measuring and Displaying Solar Panel Voltage on an LCD using Arduino

4.Displaying Weight and Force from an FSR on an I2C LCD using Arduino