10. Output Devices




💧 Flowing the energy

The objective of this week is to establish connection with some inputs to test eenrgy requirements, limitations and understand how they work

Imagen

Learning Plan:

To understand the energy flow required by an output device, it is necessary to relate it to Ohm's Law and the relationship between potential, voltage, and current.


Ohm's Law states that the current (I) flowing through a conductor between two points is directly proportional to the voltage (V) across the points and inversely proportional to the resistance (R). Expressed as V = I × R, it describes the relationship between voltage (measured in volts), current (in amperes), and resistance (in ohms) in an electrical circuit. For example, if voltage increases, current increases proportionally, assuming resistance remains constant.


💧 Research Objectives & Workflow

For the group part, tests will be conducted with a servomotor subjecting it to different voltages and resistances to understand its behavior. The personal exercise will be a test of the relationship between a button and a servo.

Imagen

💧 Learning to understand energy (Group Asignment)

1. a simple circuit powering a servo is designed to observe its behavior at 5V

Imagen

5V Feeding

2.According to the datasheet, the servo requires a suggested power of 5V. Therefore, it is connected to the 5V pin of the ESP32.

Imagen

3.3V Feeding

3. In the next exercise, we power the servo below the recommendations to evaluate its performance.

Imagen

1.8V Feeding

3. In the next exercise, we power the servo below the recommendations to evaluate its performance.

Imagen

💧 Managing movement (Personal Asingment)

This week, a brief exercise on using outputs will be conducted with the board I made the previous week. It will consist of a button that allows a servo motor to move in both directions.

Imagen

1. A second exercise was developed to validate the use of Bluetooth. In this case, the goal was to create a remote camera trigger using Bluetooth Low Energy between the Seeed board and my cellphone.

Imagen

2. This section sets up the foundation for servo control. The constant pinServo = 26 assigns pin D26 for the servo’s signal line (orange/yellow wire), ensuring it’s fixed. The variable pos (integer, starting at 0°) tracks the servo’s current angle, ranging from 0° to 180°. The boolean direccionDerecha (true) determines whether the servo moves right (increasing angle) or left (decreasing angle), starting with rightward motion. These variables allow the program to manage the servo’s position and direction dynamically.

            
DEFINE constant pinServo as 26  // Servo signal pin (D26)
SET pos as integer, initialize to 0  // Servo angle (0° to 180°)
SET direccionDerecha as boolean, initialize to true  // Movement direction (true = right, false = left)
            
            

3. This part configures the PWM signal needed for servo control and defines two helper functions. The constants period = 20000 (20 ms, 50 Hz), minPulse = 500 (0.5 ms for 0°), and maxPulse = 2500 (2.5 ms for 180°) match standard servo timing (e.g., SG90). The mapAngleToPulse function converts an angle (0° to 180°) to a corresponding pulse width (500 to 2500 µs) using linear mapping, ensuring accurate positioning. The writeServo function generates a PWM pulse by setting the pin HIGH for the calculated pulse width, then LOW for the remaining period, using precise delayMicroseconds for timing. This software PWM mimics hardware PWM, avoiding ledc issues.

            
   DEFINE period as 20000  // 20 ms period (50 Hz) in microseconds
DEFINE minPulse as 500  // 0.5 ms pulse for 0°
DEFINE maxPulse as 2500  // 2.5 ms pulse for 180°

FUNCTION mapAngleToPulse(angle):
    RETURN map(angle, 0, 180, minPulse, maxPulse)  // Convert angle to pulse width

FUNCTION writeServo(pin, angle):
    SET pulseWidth = mapAngleToPulse(angle)  // Get pulse width in µs
    SET pin to HIGH
    DELAY pulseWidth microseconds  // Hold HIGH for pulse
    SET pin to LOW
    DELAY (period - pulseWidth) microseconds  // Complete 20 ms period
            
            

4. The setup function prepares the ESP32 for servo control. It configures pin D26 as a digital output using pinMode, enabling it to send PWM signals. Setting pinServo to LOW (0V) with digitalWrite ensures no signal is sent to the servo at startup, preventing unintended movement until the loop function begins generating pulses. This establishes a safe initial state for the servo.

            
  FUNCTION setup:
    CONFIGURE pinServo as OUTPUT  // Set D26 as output
    SET pinServo to LOW  // Initialize to 0V

            
            

5. The loop function drives the servo’s continuous sweeping motion. If direccionDerecha is true, it increments pos by 1° per iteration, moving the servo rightward (toward 180°). When pos reaches 180°, it’s capped, and direccionDerecha switches to false, reversing to leftward motion. If direccionDerecha is false, pos decrements by 1° until reaching 0°, where it’s capped, and direccionDerecha switches back to true. The writeServo function sends a PWM pulse for the current pos, taking 20 ms per pulse (due to the 50 Hz period). With no additional delay, the loop runs as fast as possible, achieving maximum speed (3.6 seconds for a 180° sweep, though servos like SG90 may move faster, ~0.5-1 second, depending on physical limits).

            
  FUNCTION loop:
    IF direccionDerecha is true:
        INCREMENT pos by 1  // Move right
        IF pos >= 180:
            SET pos = 180  // Cap at 180°
            SET direccionDerecha = false  // Switch to left
    ELSE:
        DECREMENT pos by 1  // Move left
        IF pos <= 0:
            SET pos = 0  // Cap at 0°
            SET direccionDerecha = true  // Switch to right
    CALL writeServo(pinServo, pos)  // Send PWM pulse for position

            
            

5. The final result is evaluated by clicking the switch the orientation of the servo's movement

Imagen

💧 Downloadable Files

Arduino Code of Switching Direction by clickng