Arduino as Basic Tool for Robotics

A basic Arduino board, such as the Arduino Uno, is a popular microcontroller platform used for building electronics projects. Here’s a brief description of its key features and components:

Key Features of a Basic Arduino Board (e.g., Arduino Uno)

  1. Microcontroller:
    • The Arduino Uno is based on the ATmega328P microcontroller, which provides processing power and the ability to execute code.
  2. Digital Input/Output Pins:
    • The board has 14 digital pins (0 to 13) that can be configured as input or output. These pins can read or send digital signals (HIGH or LOW).
  3. Analog Input Pins:
    • It includes 6 analog pins (A0 to A5) that can read varying voltage levels. These are useful for interfacing with sensors that output an analog signal.
  4. USB Connection:
    • The board features a USB interface for connecting to a computer, allowing for programming and power supply. It can also be powered via an external power supply.
  5. Power Supply:
    • The Arduino can be powered through the USB port or an external power source (7-12V recommended) via a barrel jack or power terminal. It has built-in voltage regulators.
  6. Reset Button:
    • A reset button allows the user to restart the program running on the microcontroller without disconnecting the power.
  7. LED Indicators:
    • The board has built-in LEDs, including one connected to digital pin 13, which is commonly used for testing and basic output functions.
  8. Programming Interface:
    • The board is programmed using the Arduino IDE, which supports a simplified version of C/C++. Users write “sketches” (Arduino programs) that are uploaded to the board via USB.
  9. Form Factor:
    • The standard Arduino Uno board has a compact form factor, making it easy to integrate into various projects.
  10. Open Source:
    • Arduino boards and the software are open-source, allowing for a large community of users who contribute libraries, tutorials, and projects, making it easier for beginners to get started.

to code using C in Arduino IDE, here is a canonical sample sweep sketch:

#include <Servo.h>

Servo myServo;  // Create a Servo object

void setup() {
  myServo.attach(9);  // Attach the servo on pin 9 to the servo object
}

void loop() {
  // Sweep from 0 to 180 degrees
  for (int pos = 0; pos <= 180; pos += 1) {
    myServo.write(pos);              // Tell servo to go to position in variable 'pos'
    delay(15);                       // Wait 15 ms for the servo to reach the position
  }
  
  // Sweep from 180 to 0 degrees
  for (int pos = 180; pos >= 0; pos -= 1) {
    myServo.write(pos);              // Tell servo to go to position in variable 'pos'
    delay(15);                       // Wait 15 ms for the servo to reach the position
  }
}

blinking LED

const int ledPin = 13;  // LED connected to digital pin 13

void setup() {
  pinMode(ledPin, OUTPUT);  // Set the LED pin as an output
}

void loop() {
  digitalWrite(ledPin, HIGH);   // Turn the LED on (HIGH is the voltage level)
  delay(1000);                  // Wait for a second
  digitalWrite(ledPin, LOW);    // Turn the LED off (LOW is the voltage level)
  delay(1000);                  // Wait for a second
}

digitalWrite raw codes

#include <avr/io.h> // Include the AVR IO library for AVR microcontrollers

void digitalWrite(uint8_t pin, uint8_t value) {
    if (value == HIGH) {
        // Set the pin HIGH
        PORTB |= (1 << pin); // Assuming pin is on PORTB
    } else {
        // Set the pin LOW
        PORTB &= ~(1 << pin); // Clear the bit for the pin
    }
}

Here are some more sample codes to digest, for example, motion_servo_dance.ino:

#include <Servo.h>

Servo servo1, servo2, servo3;
const int SERVO1_PIN = 9;
const int SERVO2_PIN = 10;
const int SERVO3_PIN = 11;

const int SPEED_UP_PIN = 4;    // Speed up button
const int SPEED_DOWN_PIN = 5;  // Speed down button

const int LED_SPEED_PIN = 13;  // Built-in LED

int moveDelay = 15;              // Default delay between movements

void updateSpeed() {
  // Check speed up button
  if (digitalRead(SPEED_UP_PIN) == LOW) {
    moveDelay = max(MIN_DELAY, moveDelay - 5);
    delay(200);  // Debounce
  }
  
  // Check speed down button
  if (digitalRead(SPEED_DOWN_PIN) == LOW) {
    moveDelay = min(MAX_DELAY, moveDelay + 5);
    delay(200);  // Debounce
  }
  
  Serial.print("Speed Delay: ");
  Serial.println(moveDelay);
}

void setup() {
  servo1.attach(SERVO1_PIN);
  servo2.attach(SERVO2_PIN);
  servo3.attach(SERVO3_PIN);
  
  pinMode(SPEED_UP_PIN, INPUT_PULLUP);
  pinMode(SPEED_DOWN_PIN, INPUT_PULLUP);
  
  Serial.begin(9600);
  
  pinMode(LED_SPEED_PIN, OUTPUT);
}

void wavePattern() {
  Serial.println("Executing Wave Pattern");
  
  for(int angle = 0; angle <= 180; angle += 2) {
    updateSpeed();  // Check for speed changes
    servo1.write(angle);
    servo2.write(angle + 60 > 180 ? 180 : angle + 60);
    servo3.write(angle + 120 > 180 ? 180 : angle + 120);
    delay(moveDelay);
  }
}

void synchronizedPattern() {
  Serial.println("Executing Synchronized Pattern");
  
  for(int angle = 0; angle <= 180; angle += 2) {
    updateSpeed();  // Check for speed changes
    servo1.write(angle);
    servo2.write(180 - angle);
    servo3.write(angle);
    delay(moveDelay);
  }
}

void cascadePattern() {
  Serial.println("Executing Cascade Pattern");
  
  // First servo
  for(int angle = 0; angle <= 180; angle += 5) {
    updateSpeed();  // Check for speed changes
    servo1.write(angle);
    delay(moveDelay);
  }
  
  // Second servo
  for(int angle = 0; angle <= 180; angle += 5) {
    updateSpeed();  // Check for speed changes
    servo2.write(angle);
    delay(moveDelay);
  }
  
  // Third servo
  for(int angle = 0; angle <= 180; angle += 5) {
    updateSpeed();  // Check for speed changes
    servo3.write(angle);
    delay(moveDelay);
  }
  
  // Reset positions
  servo1.write(90);
  servo2.write(90);
  servo3.write(90);
}

void loop() {
  wavePattern();
  delay(1000);
  
  synchronizedPattern();
  delay(1000);
  
  cascadePattern();
  delay(1000);
}

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.