Pinout Reference Diagram

the pin schema, ie where the data pins are placed.

  • This is NOT the same as a schematic.

GPIO

Stands for: general purpose Input/Output

  • Input mode: pins acts as listener, reading LOW or HIGH voltage
  • Can also be for output: pin acts as sender, providing high or low voltage to turn components OFF or ON (like leds)

OUTPUT: the MCU is the boss. it provides voltage to power LEDs, Buzzers, or signals to motor controller INPUT: the MCU is the listener. it waits to see if another thing like a sensor is pressed or action is seen.

Hardware Timers

Definition: A timer is a hardware peripheral that is like a stopwatch that runs in the background.

Example of when to use two timers: When we have servos and DC motors in the same system.

  • DC motors and Servos operate at a different frequency.

Channels

channels can be thought of the workers that each read the same “timer” (stopwatch) to do their own task.

Example: imagine a race track with one starter gun (the shared timer), each runner hears the same gun and runs at the same pace but stops at the finish line at different times to do different things.

Possible Channel Pitfalls

  1. not initializing enough channels if you have 3 servos but only 1 channel, only one channel will be initialized for ONE servo. the two other servos will not respond to any commands.
  2. assigning the same channel to multiple servos if we assign the same channel to multiple servos, then each command sent to the servos will move in sync.
    • if you want this behaviour, then it is not a pitfall!

ADC

ADC stands for Analog to Digital converter

an ADC is a system that converts an analog signal (like a joystick or fingers touching a screen) to a digital signal (1s and 0s)

  • recall that a joystick consists of two potentiometers.

Bit-Rate: bit rate determines the resolution of the analog signal. If the max value of an ADC signal is 4095, that means that the signal is in 12-bit resolution

ADC and Wifi (ESP-NOW)

In my table tennis project with Connor, i learned that ESP-NOW uses the ADC2 channel pins to keep a stable connection. this means that all pins that have ADC2 capability cannot be used for anything other than wifi comms. gotta be careful. ADC1 pins are okay tho!

and don’t forget to update the pin assignments correctly!

PWM

Stands for Pulse Width Modulation

servos dont understand 0-180 degrees, but they understand PWM. most libraries assume 0 degrees = 500 and 180 degrees is 2500 for servos.

  • to get the tuning correctly, we need to do math in order to tune the correct degree rotations, especially when we have to divide the rotations into sections (like an indexer) if a motor is spazzing out (trying to move past its limits, that means that we are trying to get the motor to move past its limits which could burn out the motor, we need to limit the PWM range and ensure that if we are going back to a specific starting positions we go to that position instead of 0 or 180. (aka where we started) )

Brownout Detection

brownout detection occurs when voltage drops below the required level for reliable use of the microcontroller.

  • This is a feature to avoid damaging components

Buttons

we cant use input only GPIO pins as they are unique and lack everything else every other pin has which are: internal pull-up and pull-down resistors.

  • Pull-up resistor (DEFAULT: HIGH): this resistor is the most common. in an idle state, a button will read 1, since the default is HIGH. when a button is pressed, the press closes the circuit, the pin voltage drops to 0 and so the code will read 0.
  • Pull-down resistor (DEFAULT: LOW): this resistor is the opposite, you connect the pin to ground through a resistor. In an idle state the resistor is “anchoring” the pin to ground, which the code reads 0. When a button is pressed, the connection is closed to the voltage w note that pull-up resistors are more commonly used because the buttons are designed to “close a circuit” if we use pinMode(PIN, INPUT_PULLUP), this tells us tat the signal is stable at HIGH untill we press the button to ground it
  • on the ESP32, the GPIO pins 34 and 39 do not have an internal resistor, so INPUT_PULLUP will be ignored by the hardware causing ghost presses or unpredictable presses.
  • in a input-only pin:
    • Without a resistor: when button is not pressed, the pin is connected to nothing and it picks up electrical noise from the air (which can be unpredictable)

An internal resistor in the ESP handles the connection for us!

Regarding ESP-NOW and button presses, if we dont have a timer in software that says “ignore all changes or presses for the next 50 ms after the previous press”, then 15 or more presses might get sent at once to the slave esp, when we only want one press.

TX and RX

TX: stands for transmit (this pin or signal that is sending data to another device(s)) RX: stands for receive (this pin or signal listens for incoming data to the device)

example: UART is full-duplex

  1. TX from A connects to RX of B
  2. TX of B connects to RX of A this is so that both channels are able to communicate simultaneously

What is a MAC Address?

a mac address is a unique number for a piece of hardware

  • a MAC address does not change like an IP address ESP-NOW uses a MAC address to bypass IP handshakes. This way, an ESP can directly shout at another ESP and provide data.

Difference between #define and const int

in C, if we declare something as static, the compiler needs to know how many bytes exactly to carve out of RAM when compiling code.

const means that we have “read-only” variable and they are not “true constants” to the compiler, since a constant is still a variable that could technically change

this is where #define saves us. #define allows us to

Thread-Safe

  • CMPT201 concept! If some function is thread safe, it means that multiple threads can access it at the same time (simotaenous)
  • the API method handles the lock internally.

on the flip side, if some function/api method is NOT thread safe, it means that multiple threads cannot access the function simotaneously.

  • The only way to make the function/api method thread safe is by the programmer encasing the api call within a lock, as race conditions can occur if multiple threads access the shared resource inside that method, where the method is NOT thread safe.

Motor Drivers

Motor drivers are important when driving DC motors with voltage. The ESP32 cannot provide enough power for the DC motors, so the motor driver acts as a bridge to power the DC motors at the required voltage, assuming you have an external power supply that can supply enough power.

Duty Cycle

a duty cycle is the percentage of time a signal is HIGH in one cycle

  • however, the signal switches on/off so fast within ms that the motor sees it as as average voltage, so a duty cycle can be seen as supplying a percentage of power. Example: 50% duty cycle with 12V, therefore the motor will act like it is receiving 6V.