Skip to content

Documentation of Exercise: Connecting the LED and the Button

I wanted to program the ESP32-S3 in such a way that when we press the Built-in boost button the Built LED will start to flash and the control will simultaneously communite the word ‘awake’ with every flash of the LED

How to do this?

Setup

The first step is to setup and define the basic elements in the code. The Boost Button is set to pin 0. This is a definition or nickname. We do not need to define the LED since the Arduino code has a LED_BUILTIN command which cross references the board’s information and connects the right pin.

An alternative route would be to check the Data sheet of my specific Micro Controller and find the pin that connects to the LED.

The basic logic has to do with a need to constantly measure the status of the LED and remember whether its ‘on or off’. The Bool command sets up a definition of ‘true’ or ‘false’ that we will later define more exactly.

The unsigned long command, also sets up a variable helpful to keep track of time, which is in Milli seconds in this specific code.

The LED_BUILT in is set up as output which means the controller will send voltage to act on the LED.

The Button is set at input. Not quite sure what the pullup means or does so far.

Additionally, the Serial.Begin commad as I understand it is there to activate the connection between between the board and the monitor through the USB connection.

The system constantly checks the status on the button and time between the flashes of the LED through these two commands. ledBlinkingActive and lastBlinkTime.

My understanding of how the controller knows if the button is on or off?

By the voltage reading. If the Voltage in the 0 pin reads Positive+ then its a yes. In this case since buttons are mechanical and prone to jamming, the code has a safety (quality control mechanism built-in) which is to delay by 50millis and check again immediately. If the reading is the same then it’s confirmed that it’s a legitimate push of the button.

Based on this, the code activates the loop through ‘toggling’ between the opposite values of this command ledBlinkActive.

If the value is true - then it means its time to blink!

How does the system regulate the timing between each blink?

First it calculates the difference between ‘current time’ and the ‘time of the last blink’. If that value is equal or more than 2000 milli seconds, its the signal to blink again

Adding a message to print on the serial monitor

I tried to upgrade the code so that when ever the LED is on, the program will send a message ‘awake’ to the serial monitor.

This didn’t work at the end and I suspect the issue was with the serial monitor itself. The new code with the Serial Println would upload fine and I can verify this by the type and duration of the LED blinking. Yet, it would not show up on the serial monitor.

What I did to try to fix this:

  1. I made sure that the upload speed or baud is the same in the code as in the serial monitor. Set at 115200
  2. I diagnosed the issue to figure out where the problem is coming from. From the board or from the serial monitor. I ran other simples codes that were successfully uploads and ran on the board which made me think the issue is with the monitor.
  3. Using AI one possible problem was that the serial monitor was disconnecting before the code can run, putting my board in a prepetual loop or resting. To fix this I redid the code adding a longer delay so that would allow the code to run before the serial monitor disconnects (this is how I understood it) the AI pointed to the following code in the monitor as an issue with prepetual resting.