6. Embedded Programming

This week's practice included using the PCB I made during Week 4 . My advice I to take good care of everything you create, because I had to solder some pins again. Also, during this week, I had to watch some youtube tutorials, dust some old learnings I had and understand better the components and why I was using them.

- The group assignment: The basics of coding in c++ and python -

This week my FAB site was incredibly helpful, because It was my first time using Arduino and Python, which were the coding programs I used. The group assignment and lecture included a lot of tips and definitions for some elements around coding. Also, the page has some source code I took as my base, from the downloads section, if you want to see more you may click here .

Some basic concepts

Concept Description
Variable The option that allows you to save data and assign it a value.
Control structures: If- Else Are conditionals that state what will happen under specific contexts or situations.
Loop: For-While These state the times a cycle will repeat and under which conditions. The loop “For” states what will happen from the beginning to a specific variable. The loop “While” will occur every time the conditions are met.
Function It packs code in a simple task to complete, that can repeat many times and make the code a reusable component instead of having many lines of it.
Input State of a pin that allows it to recieve a signal from another source.
Output State of a pin that allows it to send a signal to another source.
Libraries These are available in Arduino, by adding a library you can add the details of your component and make it possible to include it in your code .
Comments Allow you to document and add notes on different sections of the code. I used them to remember the purpose of different parts of the code.
Comments in C++ (Arduino) To write single line comments you use // before the note, to write comments that go beyond a line you use /* to open and */ to close the comment section.
Comments in Python To write single line comments you use # before the note, to write comments that go beyond a line you use to open and to close the comment section.

---- Programming my board----

  1. The first step to program it, was to reset the Xiao and connect it to the computer. To do so I pressed the b button from it's lower right and it should open as an USB.
  2. Once i confirmed that it was connected, I opened Arduino. There I thought something wasn't right, because there it didn't read the Xiao as a board. The problem was that it wasn't recognizing the port, so I reset it once again, but this time pressing the R on the left corner.
  3. If you don't see the Xiao still, it's because you're using a charging cable, not one that allows you to send data. It must show the name of a board like in this picture.
  4. Once I had the port, I had to open the /“Select board” option and look for the one that said Seeed XIAO RP2040 .
  5. Now, I was ready to start programming, so I followed a blink tutorial from the Arduino official site. This was used by my local instructor as well and I copied it to the Arduino app. It's important to say that I started from the base code, but made some adjustments.
  6. The next step was checking the Xiao's pins and comparing them to a picture available on the group assignment from my fab to know their names for coding. I had to replace the name of the pin where I had a LED to make it blink. In this case I used the one called D0 everywhere that previously had the LED_BUILTIN legend on the code.
  7. Then I checked the interval between blinks. It goes where it says delay and each 1000 equals a second, if I wanted it to change the time I could play with this setting. It's important to say that this can't be 0, because that works so fast, it isn't possible to know whether it changed or stayed lit.
  8. Once that was ready, I clicked on the upload button. It made the code to start compiling on the PCB.

    Note: I had to reset my Xiao again, because It appeared as an error, now it connected again and it kept changing from time to time to be named COM3 instead of UF2

  9. Here it is with the first code!

---- Modifying the code ----

  1. Now I tried to do the same blink with all of my leds , so I used a picture of the rails behind the Xiao to get the names of their respective pins and replace them.
  2. Here it is, this one looks like christmas lights, but slower, for this video I decreased the time in the delay to 500.
  3. Having this, I reset the board again and decided to play with the button. I started to code it, so I added a variable for the state of when it's pressed that would change between HIGH while pressed and LOW when it wasn't.
  4. Be careful to check that your tags are well written, here Capital letters count for the program to read the code properly.
  5. This is how it looks with the button pressed!
  6. I also wanted to have feedback on the device telling me that this was on, so I showed the Serial Monitor .
  7. And then added the function Serial.println + the message I wanted between braces. In this case it was to tell the LED's status as I pressed the button.
  8. Important: It's also possible to use the function “Serial.print” alone, but it won't look good, because it writes the legend in the same line and also almost crashes my Arduino.

---- Programming a Neopixel Adafruit ----

  1. Then I made a new document and installed a library for the other component my board has: Adafruit Neopixel LED.
  2. After that, I opened a code available on my Fab,this works for Arduino itself, but it is also possible to adapt it.
  3. I copied and pasted it on Arduino and verified it with the verify tool. Just to check how it looked on the Xiao and understand better which variables did what.
  4. This is a video of how bright it was when I saved it to the board. It was too intense, so I looked for a function that would allow me to lower the intensity of it. (I asked chatGPT lol)
  5. So, to change it, I added the function “setBrightness() and when I asked ChatGPT it also suggested that you can change the intensity between a 0 to 255 spectrum.
  6. Here is the difference after lowering the intensity and playing with the variable values.

---- Programming a sound sensor ----

  1. I used an sound sensor model KY-037, that comes with an arduino Kit. As it is something very new to me, I followed a tutorial from Youtube to start coding. It has subtitles and was very useful, you may find it here if youre interested.
  2. The first step was to connect the sensor to my board. To do so, I connected the pins using female-female jumpers.
  3. To do so, I had to check what I was connecting to and where. During week 4, we received an image for reference to solder, this had names for the pieces that would go above, so I used it to check the traces of my board. The elements I connected first were from the side of my board: ground (G), voltage (V) and a pin from the Xiao to Transmit (Tx) the information it received.
  4. Next, I connected the pins from the sensor. The symbols were mostly the same: Voltage (V) , Ground (G) and the Digital Output (DO) .
  5. Once everything was connected with the pins, I plugged it to the computer and reset the previous codes.
  6. Then I had to identify the pins that connected directly to the sensor. To do so, I followed the route from the reference picture of the traces and also the name for analog pins that the group assignment offered.

    Note: Each picture has the pins and respective traces marked on the color of the cable I used.

  7. Now it was time to start coding. The first step was to define the Variables . I used the tag Int as in the previous codes, and gave a name To the microphone and the LED. I made them equal to the names of the pins I identified on the previous step.
  8. After that, I defined the input and output ot the signal sent.
  9. Now, It was time to create the loop of reaction and interaction between the microphone and the LED.
  10. The next step was to add a delay to make it take half a second between functions.Right after I verified the code.
  11. Here it is the first result! It worked!I made a test snapping my fingers.
  12. Even though it worked just fine, I decided to use all of my LEDs and make it mine, so the next part comes from my interpretation of what worked. I started by adding new variables for the other two LEDS.
  13. Next, I added the new LEDs as outputs.
  14. And finally, I added more reactions under the conditional else . I had to remove the spaces between the names of the variables here.

    Note: It didn't work, so I kept making some adjustments.

  15. Then I tried to use else if , but realized it didn't really work either, Because it only turned on the original Blue LED. Here I realized I needed something different.
  16. I couldn't really get why this wasn't working, so I asked chatGPT what was the problem with what I wanted. It said that the original code had an on-off logic for the original LED, but there wasn't anything to state that the LED would change with each sound, so It advised me to have a variable to define the LED in use. I called it CURRENTLED .
  17. As the inputs and outputs stayed the same, I went down to interpret the next part. Here I had to change from starting with the blue LED off, to have it on, so the loop would turn off the current LED when it heard something.
  18. Then I assigned another conditional If and else if to make the variable current led change between the three each time it captured a sound.
  19. This time It worked exactly how I wanted! So I tested it again, snapping my fingers.
  20. As you could see, the reaction was nice, but I wanted it to react better, because It had to be really close to the sound source to catch it. To increase the sensitivity of the sensor, there is a screw. The more you move it clockwise, the more sensitive the microphone will be.

    Note:After adjusting it it was ready!

Final shot!

Note: I don't own the rights of the song in the background, the source comes from here
All Rights for the song "Axel F" to Harold Faltermeyer ℗ 1984 UMG Recordings, Inc.