Week 3: Embedded Programming

Assignment: Browse through the data sheet for your microcontroller

As someone who has had extremely minimal experience in programming, I didn't know where to begin this week. I knew I wanted my weekly assignments to build up to my final assignment, but I didnt even know how a vending machine does what it does. I reached out to Niklas in the architecture section, and we talked a little about a few possible ways forward. We decided that an RP2040 mircoprocessor and keypad would be good to get started with.

Looking at the many, many pages of its data sheet, I learned that The RP2040 datasheet is long because it packs a ton of features into a tiny, affordable chip. Even the tiny pins can do so much and have so many components.

Group assignment: Demonstrate and compare the toolchains and development workflows for alternative embedded architectures

Our group assignment was also eye-opening. We were shown where to find the different electronics in the architecture shop, and we went over a few components and when to use what. We all downloaded Arduino IDE and learned the very basics.

group pic

Assignment: Write a program for a microcontroller and simulate its operation, to interact and communicate its operation

After that, I compiled a list of the components I would need to make this happen.

After that, I was almost to start the simulation. My vending machine will probably have four snack options, and therefore four metal spirals that rotate. I decided to run the simulation with just one, for simplicity purposes. I don't imagine it will be too complicated to go from one working motor to four.

Here are the steps I followed:

  1. I started by connecting the Stepper Motor to the A4988 Driver. I connected the first pair of stepper motor wires to pins 1A and 1B, and the second pair of wires to pens 2A and 2B.
  2. Next, I connected the A4988 Driver to the Arduino. I connect the STEP pin on the A4988 to pin 8 on the Arduino, and the DIR pin to pin 9.
  3. Next, I did the power connections for the driver. I connected the VMOT pin on the A4988 to a 12V power supply to power the motor, and the GND pin on the A4988 to the GND of the power supply. I didn’t connect the capacitor, because I also couldn’t find it on Wokwi, but I will definitely use one in the physical prototype. I also connected the VDD pin to the 5V pin on the Arduino to power the internal logic of the driver and connected the GND pin on the A4988 to the GND pin on the Arduino.
  4. Lastly, I connected the button to the Arduino, by connecting one side of the button to pin 2 on the Arduino and the other side to GND.
simulation circuit

Next, I worked on the code, which ChatGPT helped me with. It did have an obvious error to me, but this is what I ended up with after fixing it:

simulation code

Unfortunately, nothing happened when I ran the simulation. I'm not too sure of my hardware connections, and I imagine that that's where the error lies.

I went to office hours, and Alfonso helped me compare my simulation to an already-existing Wokwi example. This helped me get it to work! I re-wired my circuit like this:

simulation circuit

I also changed my code to the following:

second code

Right now, the motor is stepping continuously while the button is held down. So the longer I hold the button, the more steps it takes. But I want it to move a fixed number of steps each time I press the button, regardless of how long it's pressed. To achieve that, I tried changing the code so that it only registers the button press once per click, and moves a fixed number of steps every time the button is pressed and released (i.e., on a button "click" rather than holding it down). I did this by tracking the state of the button and only moving the motor when the button transitions from "not pressed" to "pressed."

third code

It's still acting a little funny, but I think this is a good spot to transfer to physical items to check out the situation clearer. It's almost perfect, but I still need each button press to turn the stepper motor the same number of rotations.

This is what it looks like with four buttons and four stepper motors:

four buttons neat

I tried to have the wires overlap as least as possible, but this is how it will look like with a more realistic setup (the buttons together and motors together):

4 buttons real

This is the code expanded to 4 buttons:


four code 1
four code 2
four code 3

And this is a video of the simulation being run: