Output Devices

This week I am driving and testing the OLED screen and the servo on the Castform board.

Servo motor

In terms of the output devices for my Castform machine, there are indeed plenty. So I started with the key part of the machine: the servo motor for form change based on the weather.

Servo wired to the Castform board
For the motor, I deviated from the original MG996R and tried an MS18 instead. I felt I may not need that much driving power, since both the plush and the arm can be fairly lightweight. This is the test setup, with the motor powered through USB.

Since the servo is driven through PWM, I needed to set up the PWM signal generation on the board. Below is the code for the PWM setup. After some document reading and tuning, I ended up with a PWM range of 500-2500 microseconds, which translates to a range of 0-180 degrees for the servo. That is exactly what I needed for form change based on the weather.

Servo pulse

#define SERVO_GPIO       1
#define SERVO_FREQ_HZ    50
#define SERVO_RES_BITS   14
#define SERVO_PERIOD_US  20000

static void servo_set_pulse(uint32_t pulse_us)
{
    uint32_t duty = (pulse_us * (1U << SERVO_RES_BITS))
                    / SERVO_PERIOD_US;

    ESP_ERROR_CHECK(ledc_set_duty(
        LEDC_LOW_SPEED_MODE,
        LEDC_CHANNEL_0,
        duty
    ));

    ESP_ERROR_CHECK(ledc_update_duty(
        LEDC_LOW_SPEED_MODE,
        LEDC_CHANNEL_0
    ));
}

PWM setup

ledc_timer_config_t timer = {
    .speed_mode = LEDC_LOW_SPEED_MODE,
    .timer_num = LEDC_TIMER_0,
    .duty_resolution = LEDC_TIMER_14_BIT,
    .freq_hz = SERVO_FREQ_HZ,
    .clk_cfg = LEDC_AUTO_CLK
};

ledc_channel_config_t channel = {
    .gpio_num = SERVO_GPIO,
    .speed_mode = LEDC_LOW_SPEED_MODE,
    .channel = LEDC_CHANNEL_0,
    .timer_sel = LEDC_TIMER_0,
    .duty = 0,
    .hpoint = 0
};

Sweep

while (1) {
    servo_set_pulse(1500);
    vTaskDelay(pdMS_TO_TICKS(3000));

    servo_set_pulse(500);
    vTaskDelay(pdMS_TO_TICKS(3000));

    servo_set_pulse(2500);
    vTaskDelay(pdMS_TO_TICKS(3000));
}
Servo moving before the arm is attached. This may be hard to see.
Servo sweeping through its range (full range is 0-180 degrees).

OLED Display

Next up I tested the other key part of the machine: the OLED display for weather and other info. This could also be a good debugging tool if I need it later. Since I am using exactly the same OLED display as my Week 3 project, I can use the same config for the OLED display.

I2C setup

#define OLED_SDA        5
#define OLED_SCL        6
#define OLED_ADDR       0x3C
#define OLED_WIDTH      128
#define OLED_HEIGHT     64

i2c_master_bus_config_t bus_config = {
    .i2c_port = I2C_NUM_0,
    .sda_io_num = OLED_SDA,
    .scl_io_num = OLED_SCL,
    .clk_source = I2C_CLK_SRC_DEFAULT,
    .glitch_ignore_cnt = 7,
    .flags.enable_internal_pullup = true
};

i2c_device_config_t dev_config = {
    .dev_addr_length = I2C_ADDR_BIT_LEN_7,
    .device_address = OLED_ADDR,
    .scl_speed_hz = 100000
};
First pass at driving the OLED.
OLED showing SUNNY on only part of the screen
SUNNY showing up among the leftover pattern on the OLED. The text display is a success but the noisy background comes from incorrect resolution settings.

Framebuffer upload

static void oled_show(void)
{
    oled_cmd(0x21);
    oled_cmd(OLED_COL_OFFSET);
    oled_cmd(OLED_COL_OFFSET + OLED_WIDTH - 1);

    oled_cmd(0x22);
    oled_cmd(0);
    oled_cmd(7);

    for (size_t i = 0; i < sizeof(framebuffer); i += 16) {
        uint8_t packet[17];
        packet[0] = 0x40;
        memcpy(&packet[1], &framebuffer[i], 16);

        ESP_ERROR_CHECK(
            i2c_master_transmit(
                oled, packet, sizeof(packet), 1000
            )
        );
    }
}

Status screen

static void show_status(const char *weather)
{
    memset(framebuffer, 0, sizeof(framebuffer));

    draw_text(16, 5, "CASTFORM", 2);

    for (int x = 8; x < 120; x++) {
        draw_pixel(x, 26);
    }

    draw_text(19, 34, weather, 3);

    oled_show();
}
OLED wired to the Castform board on the bench
Issue fixed now. Notice here the OLED is powered from the supply instead of USB. We will discuss that later.

Switching loop

oled_init();

while (1) {
    show_status("SUNNY");
    vTaskDelay(pdMS_TO_TICKS(3000));

    show_status("RAINY");
    vTaskDelay(pdMS_TO_TICKS(3000));
}
The display switching between the two weather states.

Power Consumption

Since my designated power connector just arrived this week, I have also taken the time to solder it onto the board. And this is a pretty good opportunity to test the power consumption of the board.

Castform board powered from the bench supply alone
Board powered on its own from the supply. The new connector works great.
Measuring the servo while it moves.

The passive current draw of the board is around 60mA. When the motor moves, the current draw spikes to around 360mA. Some quick math gives a peak power of about 1.8W, which is quite reasonable. However, I do expect this number to be higher once we add in the wifi, machine arms, LED lighting, etc.

Code

Download the output device code (.zip)