Week 2: Embedded Programming

The assignment this week: write a program for a microcontroller to interact and communicate.

Design

The inspiration for my project this week is my broken oven. The temperature labels have faded off the knob to set the oven temperature, so I have no idea what temperature I've even set my oven to. Wouldn't it be nice to have a device that tells me what temperature it is in there?

My digital thermometer has pretty simple circuitry and code. In theory, all I need is a thermocouple to hook up to one of the analog inputs of the RP2040, then use the program to tell the RP2040 to convert the voltage to temperature. However, the world is not ideal, so we had to add a few middle men to make the reading accurate.

For one thing, the voltage output of the thermocouple for oven temperatures is about 20 millivolts. I wanted to fill the range of my 3.3 volt microcontroller, for better resolution. Thus, we simply add an op amp to scale up the signal by a factor of 100. For ease of selecting resistors, I actually went for a factor of 101, since the gain is 1 + [ratio of the resistances], and 100:1 is an easy ratio to get.

Here's the designs for such a circuit, and the circuit itself laid out on a breadboard.

circuit designs breadboard circuit

For another thing, although I can find the type K thermocouple's voltage output for different temperatures online, the variation in resistors gave me enough pause that I wanted to run some experiments to test the actual voltage the RP2040 would be receiving at certain temperatures. At some point I would like to test it in somebody else's oven (since I don't know the temperatures in mine), but I didn't have time for that this week. Instead, I chose to test the analog output from a pot of boiling water (212 Fahrenheit) and room temperature (75 Fahrenheit), and assume a linear relationship between temperature and analog input.

Experiment

As stated above, I wanted to test out what input the microcontroller received with the thermocouple at different temperatures. I printed the analog input to the serial monitor/plotter to view the results.

experiment conditions

At room temperature (75.9 Fahrenheit), the microcontroller printed a value that oscillated between 8 and 12, most commonly settling at 10. In a pot of boiling water, however, the oscillations were a lot more volatile. They ranged from about 33 to 50, but with occasional (surprisingly evenly spaced) jumps up to about 65. It seemed to average approximately 42.

serial monitor graph

Using that for myinterpolation, I arrived at the equation:

temperature = 4.25(analog output) + 33.5

I'm still not sure if that is the best equation to use, so I intended to test it again, outputting the alleged temperatures to the serial monitor. Unfortunately, once I tried using that equation, the breadboard for my circuit suddenly got very particular, and would only respond to changes in the temperature when the wires were held at the proper angles. Perhaps I'll perfect this project when we do PCBs, so I won't have to worry about finnicky wires. Once I do have a reliable readout, I'll get some heat resistant tape, and wire the thermocouple up in my oven, to clip to the microcontroller whenever I need to know how hot it is.

So, without having thoroughly tested it, here is the code I wrote.

Fabriation Obstacles

  • The USB-C cable I initially used to connect my RP2040 to my computer wouldn't do information transfer
  • I initially mixed up the pins of the voltage supply and the non-inverting input on my op amp.
  • The op amp I used initially (LM741) had a mysterious null offset pin that I didn't expect was necessary to hook up to anything. but apparently it needs to be utilized if you want it to work like a regular op amp. I initially thought I had fried the op amp by mixing up the pins earlier, but the same odd behavior persisted on a fresh op amp, so I decided to switch to a LM358 instead.
  • Once I was running experiments with the thermocouple readout, the wires on my breadboard suddenly became very particular, and nigh impossible to send anything reliable to the microcontroller.
  • Back to home