Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# PlatformIO files
.pio/
.vscode/
.pioenvs/
.piolibdeps/

# Build artifacts
*.pyc
__pycache__/
.cache/

# IDE files
.DS_Store
*.swp
*.swo
*~
118 changes: 117 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,117 @@
# icebreakers
# ESP32 4-Servo Controller

This repository contains code for programming an ESP32 microcontroller to control 4 microservos continuously in a loop pattern.

## Overview

The program makes 4 servos sweep back and forth continuously between 0° and 180°, creating a smooth looping motion. Each servo operates independently and can be customized.

## Hardware Requirements

- ESP32 development board (ESP32-DevKitC or compatible)
- 4x Microservos (SG90 or similar)
- Power supply for servos (5V, adequate current for 4 servos - typically 2A+)
- Jumper wires
- Breadboard (optional)

## Wiring Connections

Connect the servos to the following GPIO pins on the ESP32:

| Servo | GPIO Pin |
|-------|----------|
| Servo 1 | GPIO 18 |
| Servo 2 | GPIO 19 |
| Servo 3 | GPIO 21 |
| Servo 4 | GPIO 22 |

**Important:**
- Connect all servo signal wires to the respective GPIO pins
- Connect all servo ground (brown/black) wires to ESP32 GND
- Connect all servo power (red) wires to external 5V power supply
- Connect ESP32 GND to power supply GND (common ground)

## Software Requirements

### Install PlatformIO

This project uses PlatformIO for dependency management and building. You can use PlatformIO with:

#### Option 1: PlatformIO IDE (VS Code Extension)
1. Install [Visual Studio Code](https://code.visualstudio.com/)
2. Install the PlatformIO IDE extension from VS Code marketplace
3. Open this project folder in VS Code

#### Option 2: PlatformIO CLI
```bash
# Install PlatformIO CLI
pip install platformio

# Navigate to project directory
cd icebreakers
```

## Dependencies

The project automatically installs the following dependencies (configured in `platformio.ini`):
- **ESP32Servo** - Servo control library optimized for ESP32

These will be automatically downloaded when you first build the project.

## Building and Uploading

### Using PlatformIO IDE (VS Code)
1. Open the project folder in VS Code
2. Click the checkmark icon (✓) in the bottom toolbar to build
3. Click the arrow icon (→) in the bottom toolbar to upload to ESP32

### Using PlatformIO CLI
```bash
# Build the project
pio run

# Upload to ESP32 (make sure ESP32 is connected via USB)
pio run --target upload

# Monitor serial output (optional)
pio device monitor
```

## Customization

You can modify the following parameters in `src/main.cpp`:

- **Servo pins**: Change `SERVO_PIN_1` through `SERVO_PIN_4` constants
- **Angle range**: Modify `MIN_ANGLE` and `MAX_ANGLE` (default: 0-180°)
- **Speed**: Adjust `STEP_DELAY` (milliseconds between steps)
- **Smoothness**: Change `ANGLE_STEP` (degrees per step)

## Serial Monitor

The program outputs servo positions to the serial monitor at 115200 baud. You can view this for debugging:

```bash
pio device monitor --baud 115200
```

## Troubleshooting

- **Servos jittering**: Ensure adequate power supply for all servos
- **Servos not moving**: Check wiring connections and GPIO pin assignments
- **Upload fails**: Make sure ESP32 is properly connected and correct COM port is selected
- **Build errors**: Run `pio lib install` to ensure all dependencies are installed

## Project Structure

```
icebreakers/
├── src/
│ └── main.cpp # Main Arduino sketch
├── platformio.ini # PlatformIO configuration & dependencies
├── .gitignore # Git ignore file
└── README.md # This file
```

## License

This project is open source and available for educational and personal use.
17 changes: 17 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
; PlatformIO Project Configuration File
;
; Build options: build flags, source filter
; Upload options: custom upload port, speed and extra flags
; Library options: dependencies, extra library storages
; Advanced options: extra scripting
;
; Please visit documentation for the other options and examples
; https://docs.platformio.org/page/projectconf.html

[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
ESP32Servo
monitor_speed = 115200
106 changes: 106 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* ESP32 4-Servo Continuous Loop Controller
*
* This program controls 4 microservos connected to an ESP32,
* moving them continuously in a loop pattern.
*
* Connections:
* - Servo 1: GPIO 18
* - Servo 2: GPIO 19
* - Servo 3: GPIO 21
* - Servo 4: GPIO 22
*
* The servos will sweep back and forth continuously.
*/

#include <Arduino.h>
#include <ESP32Servo.h>

// Define servo pins
const int SERVO_PIN_1 = 18;
const int SERVO_PIN_2 = 19;
const int SERVO_PIN_3 = 21;
const int SERVO_PIN_4 = 22;

// Create servo objects
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

// Servo parameters
const int MIN_ANGLE = 0;
const int MAX_ANGLE = 180;
const int STEP_DELAY = 15; // Delay in ms between steps
const int ANGLE_STEP = 1; // Degrees to move per step

// Current positions
int pos1 = MIN_ANGLE;
int pos2 = MIN_ANGLE;
int pos3 = MIN_ANGLE;
int pos4 = MIN_ANGLE;

// Direction flags (1 = increasing, -1 = decreasing)
int dir1 = 1;
int dir2 = 1;
int dir3 = 1;
int dir4 = 1;

void setup() {
// Initialize serial communication for debugging
Serial.begin(115200);
Serial.println("ESP32 4-Servo Controller Starting...");

// Attach servos to pins
servo1.attach(SERVO_PIN_1);
servo2.attach(SERVO_PIN_2);
servo3.attach(SERVO_PIN_3);
servo4.attach(SERVO_PIN_4);

// Initialize servos to start position
servo1.write(pos1);
servo2.write(pos2);
servo3.write(pos3);
servo4.write(pos4);

Serial.println("Servos initialized. Starting continuous loop...");

delay(1000); // Wait 1 second before starting
}

/**
* Update a single servo's position in the continuous loop
* @param servo Reference to the Servo object to update
* @param pos Reference to current position (modified by this function)
* @param dir Reference to current direction (modified by this function)
*/
void updateServo(Servo &servo, int &pos, int &dir) {
pos += dir * ANGLE_STEP;
if (pos >= MAX_ANGLE) {
pos = MAX_ANGLE;
dir = -1;
} else if (pos <= MIN_ANGLE) {
pos = MIN_ANGLE;
dir = 1;
}
servo.write(pos);
}

void loop() {
// Update all servos
updateServo(servo1, pos1, dir1);
updateServo(servo2, pos2, dir2);
updateServo(servo3, pos3, dir3);
updateServo(servo4, pos4, dir4);

// Small delay between updates
delay(STEP_DELAY);

// Optional: Print positions every 100 iterations for debugging
static int counter = 0;
counter++;
if (counter >= 100) {
Serial.printf("Positions - S1:%d S2:%d S3:%d S4:%d\n", pos1, pos2, pos3, pos4);
counter = 0;
}
}