Week 12 Output devices

LCD OLED SSD1306

This is the group assignment Website Fab Lab Puebla

  • Group assignment:
    • Measure the power consumption of an output device.
    • Document your work on the group work page and reflect on your individual page what you learned
  • Individual assignment:
    • Add an output device to a microcontroller board you’ve designed and program it to do something.

From the things that our instructor said, here are some definitions and statements that i learned and you shoul know before starting this task.

We needed to understand how power worked, what we are measuring and the relation between this forces in electricityy first let's define the main variables in a circuit Current(I), Voltage(V), Resistance(R) and power(P).

  • Power The amount of energy used or transferred per unit of time. Think of it like the strength of an electrical flow - higher power means more energy being used or transferred.
  • Voltage Often likened to electrical pressure, voltage is the force that pushes electrical charges through a conductor (like a wire). Higher voltage means more force, like higher water pressure in a hose pushing water through.
  • Current This is the flow of electrical charge carriers, usually electrons, through a conductor. Imagine it as the rate of flow of electricity in a circuit - higher current means more electrons flowing per second.
  • Resistance Resistance opposes the flow of electrical current. It's like friction in a pipe that reduces the flow of water. Materials with high resistance require more voltage to produce the same current flow.
  • Ohm's Law Wheel Explanation

    This image is called an Ohm’s Law Wheel, and it helps explain the relationships between power (P), voltage (V), current (I), and resistance (R) in electrical circuits. It’s divided into sections that provide formulas depending on which two of these four quantities you know and which one you want to calculate.

    Key Terms:
    • Power (P): Measured in watts (W), it indicates how much energy is being used or transferred.
    • Voltage (V): Measured in volts (V), it's the electrical pressure or potential difference.
    • Current (I): Measured in amps (A), it’s the flow of electrical charge.
    • Resistance (R): Measured in ohms (Ω), it shows how much the material opposes the flow of current.
    How to Use It:

    Start by identifying what you know and what you need to find out. Each quadrant shows formulas related to specific parameters.

    Examples:
    • If you know Voltage (V) and Resistance (R): Use I = V / R to find Current.
    • If you know Power (P) and Voltage (V): Use I = P / V to find Current.
    Wheel Structure:
    • Top left (green section): Deals with Power.
      Examples: P = V × I or P = V² / R
    • Top right (pink section): Focuses on Voltage.
      Examples: V = I × R or V = √(P × R)
    • Bottom left (yellow section): Focuses on Current.
      Examples: I = P / V or I = √(P / R)
    • Bottom right (blue section): Focuses on Resistance.
      Examples: R = V / I or R = V² / P
    Why It’s Useful:

    This wheel simplifies calculations in electrical circuits by letting you choose the right formula based on what you know. It’s handy for students, engineers, or anyone working with electronics.

    Steps to Connect an OLED Display to XIAO RP2040 (first board)

    To do this assigment i used the board that i had used in the Inputs Task here you can take a view of it.

    1. Identify the pins on the OLED display:
    • GND: Ground
    • VCC: Power supply voltage (3.3V or 5V)
    • SCL: Clock line (Serial Clock)
    • SDA: Data line (Serial Data)
    2. Identify the pins on the XIAO RP2040 board:
    • GND: Ground
    • 3V3 (or 5V): Power supply voltage
    • SCL: Pin labeled as SCL (default D4)
    • SDA: Pin labeled as SDA (default D5)
    3. Connect the wires:
    • GND (display) to GND (XIAO RP2040)
    • VCC (display) to 3V3 (XIAO RP2040) or 5V
    • SCL (display) to SCL (XIAO RP2040) (D4)
    • SDA (display) to SDA (XIAO RP2040) (D5)
    4. Configure the software:

    Use a library like Adafruit_SSD1306 or U8g2 to manage the OLED display.

    Make sure to configure the I2C address of the OLED correctly in the code (typically 0x3C).

    5. Basic code to test:
                                
                                #include <Wire.h>
                                #include <Adafruit_GFX.h>
                                #include <Adafruit_SSD1306.h>
                                
                                #define SCREEN_WIDTH 128
                                #define SCREEN_HEIGHT 64
                                #define OLED_RESET -1
                                Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
                                
                                void setup() {
                                    if (!display.begin(SSD1306_I2C_ADDRESS, 0x3C)) { 
                                        for (;;); // Pantalla no encontrada
                                    }
                                    display.display();
                                    delay(2000);
                                    display.clearDisplay();
                                    display.setTextSize(1);
                                    display.setTextColor(SSD1306_WHITE);
                                    display.setCursor(0, 10);
                                    display.println("Hello World");
                                    display.display();
                                }
                                
                                void loop() {
                                }
                                
                              

    Proof that how its must work.

    Finally i make a code that counts 1 + 1 every one second and make a format to show by column, this format will help me in m final project where i will have differents measures of Absorbence.

                                
                                  #include 
                                    #include 
                                    #include 
                                    
                                    // Definición de ancho y alto de la pantalla
                                    #define SCREEN_WIDTH 128
                                    #define SCREEN_HEIGHT 64
                                    
                                    #define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)
                                    #define SCREEN_ADDRESS 0x3C // Dirección I2C estándar para la pantalla OLED
                                    
                                    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
                                    
                                    // Valores iniciales de absorbancia
                                    float absorbancia[] = {1, 2, 3, 4, 5, 6};  // Valores iniciales
                                    
                                    void setup() {
                                      display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);  // Inicializar la pantalla OLED
                                      display.clearDisplay();  // Limpiar el buffer
                                      display.display();  // Actualizar pantalla inicial
                                    }
                                    
                                    void loop() {
                                      // Actualizar y mostrar los datos en la pantalla
                                      mostrarAbsorbancia();
                                    
                                      delay(1000);  // Actualizar cada 2 segundos
                                    }
                                    
                                    // Función para mostrar los datos en la pantalla
                                    void mostrarAbsorbancia() {
                                      display.clearDisplay();  // Limpiar la pantalla
                                    
                                      // Mostrar título y línea separadora
                                      display.setTextSize(1);
                                      display.setTextColor(SSD1306_WHITE);
                                      display.setCursor(10, 0);  // Posición para el título
                                      display.println(F("Absorbancia (nm)"));
                                      display.drawLine(0, 10, SCREEN_WIDTH, 10, SSD1306_WHITE);  // Línea horizontal
                                    
                                      // Mostrar cada valor con su columna
                                      for (int i = 0; i < 6; i++) {
                                        display.setCursor(0, 15 + (i * 10));  // Posición vertical ajustada para cada fila
                                        display.print(F("- Columna "));
                                        display.print(i + 1);
                                        display.print(F(": "));
                                        display.println(absorbancia[i], 4);  // Mostrar valor con 4 decimales
                                      }
                                    
                                      display.display();  // Mostrar contenido actualizado en la pantalla
                                    
                                      // Incrementar los valores secuenciales
                                      actualizarAbsorbancia();
                                    }
                                    
                                    // Función para incrementar secuencialmente los valores de absorbancia
                                    void actualizarAbsorbancia() {
                                      for (int i = 0; i < 6; i++) {
                                        absorbancia[i] += 1;  // Incrementar cada valor en 1
                                      }
                                    }
                                
                              

    Video

    Here i have a video that could help you to understand more of how to programmed LCD SSD1306 on You Tube

    Second board: Xiao RP2040 to control NEMA 17

    I did a second task just to make a few trials of how to connect the microcontroller with a Nema 17 Motor DC, expecting to make a progress of how i will connect and control my CNC of my final project

    For this assigment i used the board that i fabricated on my week of Electronics design take a look if you want to check the pcb or schematics

    Steps to Connect NEMA 17 with A4988 Driver to XIAO RP2040
    1. Identify the Pins on the A4988 Driver
    • VMOT: Motor power supply (8-35V)
    • GND: Ground for motor power
    • VDD: Logic power supply (3-5.5V)
    • STEP: Step signal (controls the motor's steps)
    • DIR: Direction signal (controls the rotation direction)
    • ENABLE: (optional) Enables or disables the driver
    • MS1, MS2, MS3: Set microstepping mode (optional)
    • 2A, 2B, 1A, 1B: Connect to stepper motor coils
    2. Identify the Pins on XIAO RP2040
    • Select any digital pins for STEP and DIR signals
    • Use the 3V3 pin for logic power
    • Use the GND pin for ground
    3. Connect the Power Supply
    • Connect VMOT on the A4988 to the positive terminal of your motor power supply (8-35V)
    • Connect GND on the A4988 to the ground terminal of the motor power supply
    • Add a 100 µF capacitor between VMOT and GND (to prevent voltage spikes)
    4. Connect Logic Power
    • Connect VDD on the A4988 to the 3V3 pin of the XIAO RP2040
    • Connect GND on the A4988 to the GND pin of the XIAO RP2040
    5. Connect Control Signals
    • STEP pin (A4988) → Connect to any digital output pin on the XIAO (e.g., D2)
    • DIR pin (A4988) → Connect to another digital output pin on the XIAO (e.g., D3)
    • (Optional) ENABLE pin (A4988) → Connect to another digital pin or leave unconnected
    6. Connect the Motor to A4988
    • 1A and 1B → Connect to one coil of the NEMA 17
    • 2A and 2B → Connect to the other coil of the NEMA 17
    7. Optional: Microstepping Configuration

    To use microstepping, connect MS1, MS2, and MS3 to logic HIGH or LOW:

    • LOW LOW LOW: Full step
    • HIGH LOW LOW: Half step
    • HIGH HIGH HIGH: 1/16 step
    EXample CODE
                                  
                                  #define STEP_PIN 2
                                  #define DIR_PIN 3
                                  
                                  void setup() {
                                      pinMode(STEP_PIN, OUTPUT);
                                      pinMode(DIR_PIN, OUTPUT);
                                  
                                      digitalWrite(DIR_PIN, HIGH); // Set rotation direction
                                  }
                                  
                                  void loop() {
                                      digitalWrite(STEP_PIN, HIGH);
                                      delayMicroseconds(500); // Step pulse duration
                                      digitalWrite(STEP_PIN, LOW);
                                      delayMicroseconds(500); // Step delay
                                  }
                                  
                                
    MY CODE

    This is the code i use for making trials on the NEMA 17 where i wrote to move just 500 steps to the right.

                                  
                                    #include 
    
                                      #define DIR_PIN 8
                                      #define STEP_PIN 9
                                      #define ENABLE_PIN 10
                                      #define SLAVE_ADDRESS 0x04
                                      #define STEPS_PER_CM 50 // Pasos necesarios para mover 1 cm
                                      
                                      void setup() {
                                        // Configurar los pines como salida
                                        pinMode(DIR_PIN, OUTPUT);
                                        pinMode(STEP_PIN, OUTPUT);
                                        pinMode(ENABLE_PIN, OUTPUT);
                                      
                                        // Iniciar comunicación I2C
                                        Wire.begin();
                                      
                                        // Habilitar el controlador de motor (activo bajo)
                                        digitalWrite(ENABLE_PIN, LOW);
                                      
                                        // Establecer dirección a HIGH para avanzar
                                        digitalWrite(DIR_PIN, HIGH);
                                      }
                                      
                                      void loop() {
                                        // Avanzar 10 cm (500 pasos)
                                        for(int x = 0; x < 500; x++) {
                                          digitalWrite(STEP_PIN, HIGH);
                                          delayMicroseconds(3000); // Ajustar delay para velocidad deseada
                                          digitalWrite(STEP_PIN, LOW);
                                          delayMicroseconds(3000); // Ajustar delay para velocidad deseada
                                        }
                                        delay(5000); // Pausa de 5 segundos
                                      
                                        // Enviar señal al otro XIAO
                                        Wire.beginTransmission(SLAVE_ADDRESS);
                                        Wire.write("1"); // Enviar señal para encender el LED
                                        Wire.endTransmission();
                                      
                                        // Detener el motor después de 1 ciclo
                                        while(true);
                                      }
                                  
                                
    Connection of the board with a4988 drivers

    Video

    Files
  • Code1
  • Code2
  • DatasheetLCD