Skip to content

6. Embedded Programming

This week we dabbled in coding of the Microcontroller board.

Since I am using the arduino IDE, the programming language is C++ language. IDE stands for Integrated Development Environment. Basically, it is a software you can use to write, compile, and upload code to an Arduino UNO board or any other Microcontroller.
I prefer to use the Arduino IDE as it is easy to use. The interface is very straightforward with buttons to compile, upload, and manage your code.

The IDE uses a simplified version of C++ that’s perfect for controlling hardware like sensors, motors, LEDs etc. There are also tons of built-in libraries that make it simple to add complex features.

Setting up of RP2040 on Arduino IDE:

I followed the steps as shown in Adrian’s Quentorres documentation and connected my Xiao RP2040 to the Arduino IDE.

Steps: 1. Refer to Adrians Doc- LINK

  1. Go to File > Preferences > Add URL

  2. Add the URL given on Adrians doc. For convinience, here it is - https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

  3. Install the library

  4. Select the right board

  5. For the RGB lights, install the library too

  6. Additional notes:
    If you get a port error such as this-

Head over to your Computer’s Device Manager and look for the USB serial device COM

Now, you can use this COM in the IDE to identify the right port for connecting the MC.
Once you run the code successfully, this folder pops up on its own and closes within a few seconds.

Keep in mind that you need to type into the serial monitor input bar in order for the RGB to flash its colors. The more your typed input, the longer your RGB will blink.

Programming:

Spiral 1

From there on, I referred to Neil’s RP2040 Blink and RGB code and began experimenting with the code to modify the RGB neopixel on the Xiao board.

This is the RGB light:

Neil’s Blink code for RGB

I did not make any changes to it and used it as a test code.

Spiral 2

Next, I changed the color values in the code so that I could get white light as outcome:

Changing the values of the R, G, and B colors in the pixel.setPixelColor(0,pixel.Color(R, G, B)) line of code would produce a different light color.

The RGB value for white light is 255 Red, 255 Green, 255 Blue. Hence, I altered the numbers to give white light:

pixel.setPixelColor(0,pixel.Color(255,255,255));
pixel.show();
delay(200); 

Having this in the loop would cause the white light to stay on continuously without blinking.

However, to get it to blink, we need to set the color to black [R=0, G=0, B=0] and give it a delay:

pixel.setPixelColor(0,pixel.Color(0,0,0));
pixel.show();
delay(200); 

Here, the LED turns off as 0, 0, 0 corresponds to black light or OFF.

White light blink

Spiral 3

I then added a button to a breadboard and played around with the button code:

Only an LED:

With 1 LED and button:

Button and 1 LED Example code

Here, the Push Button has a ‘state’ and a ‘pin number’ for the signal. The button is connected between the Signal Pin and the 5V or 3V3 such that when the button is pressed and the current flows through from the the voltage pin to the signal pin, it is read as ON. Otherwise the Button ‘State’ is OFF as declared in the line int buttonState = 0;.

Spiral 4

With 2 LED and button:

Button and 2 LEDs

Here, I used two LEDs. In the previous spiral, I had used one LED and the code from examples. Here, I retained most of the code and added another Led “ledPin2”.

Further, I declared that the second led will be connecting to GPIO pin 1 in the line const int ledPin2 = 1;.
Then, in set up, I initialized the two Led pins “ledPin” and “ledPin2” as Output while the “buttonPin” remained as Input.

pinMode(ledPin2, OUTPUT);

Finally, in the If statement, I added that the voltage on BOTH pins should be HIGH when the Button is ON.

Spiral 5

Using the QT and the inbuilt button to light an external LED

Here, I used the same code as the Button and 2 LEDs, except I changed the Pin numbers on one of the LedPins to match the LED pin which was inbuilt on the quentorres. This was the LED connected to D0.
Next, I connected an external LED on a breadboard and ran jumpers from the Ground and a signal pin of the Quentorres to this external LED. I also attached a resistor so that the LED does not burn out because of too much current.

Code

Neils Blink code for RGB
White light blink
Button and 1 LED
Button and 2 LEDs

Final Project Programming:

In my final project, I mainly utilized LED PWM to achieve different light effects such as lighting and fire.

I have explained the same better in my Final Project: Development page however, I will quickly run it over here as well.

Lightning effect done using LED PWM:

FP Lightning code

Explanation:

I wanted a lightning-like effect with the LED. For this, I first understood the graph of what I wanted.

Then I began coding. I played around with the fadevalues and the timings of each delay until I got something that I liked.

In the first loop, which occurs thrice, the Lightning Phase is taking place. Each Lightning phase has two pulses. The first pulse has a short and quick rise and fall, whereas the second pulse has a quick rise and a slow fall.

Before each lightning occurs, there is a 8 to 12 second delay.

Once the Lightning Phase has occured three time, thanks to the for loop, we move on to the Flickering Phase. This utilizes a loop to simply gain brightness. The LED fades in really slow:

analogWrite(ledPin, fadeValue + random(-100, 100)); where fadeValue gradually increases. The random here contributes to the “flicker” of the LED as it gets brighter. This was done so that it appears as if it is gathering “magical energy”.

And then finally turning off and repeating the whole thing.

I have added relevant comments in the code above talking about which exact line of code does what to achieve the Lightning!

Group Assignment

Find this week’s group work on Tejas Dandge’s Fab page [Source of comparison: ChatGPT]

Our conclusion (As mentioned on Tejas’ page as well):

After the comparison we concluded that each microcontroller has its strengths and is suited for different types of applications. The RP2040 offers high performance and versatility, the ATTiny44 is more cost-effective and suitable for simpler tasks, while the ESP8266 provides built-in Wi-Fi connectivity, making it ideal for IoT applications requiring internet connectivity. The choice depends on the specific requirements and constraints of your project.

Personal conclusion:

After working with a few different microcontrollers over the 20 weeks, I will tell you what I think of them:
1. Arduino UNO: Although this microcontroller board is not allowed for Fab, I have used it in my second year of college. This board was my entrance into electronics and physical computing and it is excellent for just that! It is probably the most user friendly of all boards as it has pins to attach jumpers and required no soldering. The only downside is the size of the board and the cost. It is a general purpose breakout board which can be used for a variety of projects, however, in the commercial world, you CANNOT use an arduino inside a product (such as a lamp that has certain special light effects) as it is expensive and will take up space. Here, what you will need is a PCB and a smaller microcontroller such as the ATTiny series.
2. Xiao RP2040: This is the board we used for almost all of our Fab projects. It is small and reliable and can easily be soldered onto a PCB. However, the downside again is cost along with the fact that it only has a total of 14 pins.
3. Xiao ESP32 S3: I used this board in my Week 14 Networking along with Week 4 Electronics Production where I attached it to the Quentorres. This board is also small and equally reliable. However, it needs to be pushed into bootloader mode everytime you want to upload a code. This was a problem even Tejas faced during his Final Project. The ESP32, however, does support wireless communication over wifi/bluetooth and I tried exploring this in my Networking week. However, I did not succeed entirely in doing so.
4. NodeMCU with the ESP8266: Again, this is a commercial board that I used in my Networking Week along with my Interfaces Week (W15). It communicates really well over wifi/bluetooth and is also very easy to use.