The primary mission is the same for every team: measure air temperature and air pressure during the descent, transmit the readings to your ground station once a second, then analyse them and produce graphs. The descent takes roughly 20 to 40 seconds, which means you get 20 to 40 packets, once, with no retakes.
| What the mission needs | Which tutorial did it |
|---|---|
| Readings from a sensor | 7, the BMP280 |
| Exactly one set per second | 4, the millis() pattern |
| Readings turned into characters | 3, Serial.print |
| A tidy, named job | 5, your own functions |
| Something a spreadsheet can open | This one |
This tutorial puts those together. The sketch below runs today on a breadboard and does the primary mission from end to end. Your real flight software will add a radio and your own secondary mission on top, and the structure stays the same.
Design first
What goes in a packet
Before writing any code, decide what one packet contains. It is a design decision and it belongs in your report, not something to improvise at 2 am.
| Field | Why it is there |
|---|---|
| Packet number | Counts up by one. If the ground station sees 14 then 16, you know exactly what you lost and when. Without it you cannot tell a dropped packet from a slow one. |
| Time | millis() since power up. Lets you plot anything against time and check your rate was really one per second. |
| Temperature | Required by the mission. |
| Pressure | Required by the mission. Store it raw, in pascals, and work out altitude later. |
| Your secondary mission data | Whatever you chose. Add columns, keep the order fixed. |
Then decide how to separate the fields. Commas are the usual answer, because a comma separated line opens directly in any spreadsheet and is readable by a human watching the Serial Monitor. Pick one order and never change it, because your ground station will be written to expect it.
packet,time_ms,temp_C,pressure_Pa says what each column is and what unit it is in. Printing that header once at startup costs you nothing and means a file found three weeks later still makes sense. Units in the column name is a good habit: it is how you avoid finding out afterwards that someone assumed hectopascals.
The sketch
The whole thing, line by line
// Everything from the first seven tutorials, doing the primary mission.
// One line of comma separated telemetry every second, with a packet
// number so you can tell when one goes missing.
#include <Wire.h>
#include <Adafruit_BMP280.h>
Adafruit_BMP280 bmp;
const unsigned long SEND_EVERY_MS = 1000; // once per second
unsigned long lastSend = 0;
unsigned long packet = 0;
void setup() {
Serial.begin(9600);
delay(500);
if (!bmp.begin(0x76)) {
Serial.println("# BMP280 not found");
while (1) {
delay(100);
}
}
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,
Adafruit_BMP280::SAMPLING_X2,
Adafruit_BMP280::SAMPLING_X16,
Adafruit_BMP280::FILTER_X16,
Adafruit_BMP280::STANDBY_MS_63);
// A header line so a spreadsheet knows what the columns mean.
Serial.println("packet,time_ms,temp_C,pressure_Pa");
}
void loop() {
unsigned long now = millis();
if (now - lastSend >= SEND_EVERY_MS) {
lastSend = now;
packet++;
sendTelemetry(packet, now);
}
// Everything else your CanSat does goes here: a recovery beacon,
// a second sensor, a command from the ground. Nothing is blocked,
// because nothing above this line waits.
}
// One job, one function. Swap the Serial lines for radio lines
// later and nothing else in the sketch has to change.
void sendTelemetry(unsigned long number, unsigned long timeMs) {
float tempC = bmp.readTemperature();
float pressurePa = bmp.readPressure();
Serial.print(number);
Serial.print(",");
Serial.print(timeMs);
Serial.print(",");
Serial.print(tempC, 2);
Serial.print(",");
Serial.println(pressurePa, 0);
}
Line by line
const unsigned long SEND_EVERY_MS = 1000;- The rate, in one place, named, and marked
constso nothing can change it by accident. Capital letters for constants is a convention, not a rule. unsigned long packet = 0;- Outside both functions, so it survives between passes.
unsigned longbecause it only ever climbs. if (now - lastSend >= SEND_EVERY_MS)- The timing gate from tutorial 4. Everything inside happens once a second. Everything outside happens thousands of times a second, and that is what you want for anything that has to react quickly.
sendTelemetry(packet, now);- One named job, called from one place. Your block diagram probably has a box called telemetry; this is that box.
Serial.println("# BMP280 not found");- A leading
#marks a line as a comment rather than data, which most plotting tools and spreadsheets understand. Now your error messages do not corrupt your data file. Serial.print(tempC, 2);- Two decimal places. The sensor is accurate to about a degree, so printing six decimals would be claiming precision you do not have.
Serial.println(pressurePa, 0);- No decimals. Pressure in pascals is a five or six digit number and the fraction is noise.
Serial.print(",");- The separator. Four fields means three commas, then one
printlnto end the line. Get this wrong and every line runs into the next.
Afterwards
Getting the data off the board
The numbers are in the Serial Monitor. Now get them somewhere you can graph them, which is the part of the mission the report actually marks.
| Way | How | Good for |
|---|---|---|
| Copy and paste | Select the Serial Monitor output, paste into a text file, save it as .csv, open in a spreadsheet. | Bench tests today. Fine for the first graph you ever produce. |
| Log to a card on board | A microSD module and the SD library. Write the same line to a file instead of, or as well as, the serial port. | Insurance. Recovery is not guaranteed, but if you do get the can back you have every reading at full rate. |
| Receive by radio | The ground station reads a serial port on your laptop and writes the lines to a file as they arrive. | The actual mission. A short Python script is enough. |
Make this a real decision rather than a default. If you only transmit and the can is never found, you still have your data. If you only store and the can is lost, you have nothing. If you do both, you spend mass, power and a few milliseconds per packet. The competition says plainly that recovery is not guaranteed, so this is a genuine design choice, and reviewers like to see your reasoning for it.
Next step
Swapping the USB cable for a radio
Notice that every line that touches the outside world lives inside sendTelemetry(). When you add a radio, you rewrite that one function. The sensor code, the timing and the packet counter all stay exactly as they are.
Some radio modules, such as the HC-12 or APC220, look like a serial port to your sketch. For those, you swap Serial for the name of a second serial port. Others, such as LoRa modules, want a complete packet in one go, so you build the line in memory first and then transmit it. Either way, one function changes.
The competition requires that your frequency can be changed, and that you stay within UK regulations. Licence free use at 433 MHz is limited in both power and how much of the time you may transmit. Several teams will be transmitting on launch day, so solve this early: work out how you will change channel with the can sealed, then test it.
Honest advice
The Uno is 68.6 mm long
One last thing about the board itself. An Arduino Uno is 68.6 mm long. The inside of the can is 66 mm across. It will not lie flat, however you turn it.
For learning, that does not matter. The Uno is sturdy, hard to damage, and every example on the internet assumes one. For flying it matters a lot, so most teams learn on an Uno and fly something smaller: a Nano, a Pico or similar. The good news is that almost everything in these eight tutorials moves across unchanged.
| Moving to a Pico | What changes |
|---|---|
| The language | Nothing, if you use the Arduino core for the Pico. The same C++ sketches compile. MicroPython is also an option and is a different language to learn. |
| Pin numbers | Different names and different positions. Your const int declarations are the only lines that should need editing, which is why they are at the top. |
| I2C pins | Configurable rather than fixed, so you choose them. Still two wires. |
| Voltage | 3.3 volts throughout, which makes most sensors easier rather than harder. |
int size | Four bytes instead of two, so numbers that overflowed on the Uno no longer do. Do not rely on that: write it correctly on both. |
| Memory | Far more of it, so a String is less dangerous. Still best avoided in flight code. |
Dimensions and rules quoted here come from the UK CanSat Competition Guidelines and the ESA CanSat Book. Check the current year's documents before you rely on any of them.
Now change it
Change one thing at a time, upload, and watch what the board does.
- Run the sketch and let it collect two minutes of data. Save it as a
.csvand plot pressure against time in a spreadsheet. - Carry the breadboard up a flight of stairs while it is logging. Can you see the stairs in the graph?
- Add a column for altitude above your starting point. You will need a reading taken before you move.
- Change
SEND_EVERY_MSto 500 and then to 100. At what rate does the board start to fall behind, and how can you tell from thetime_mscolumn? - Add a packet counter check at the other end: look through your saved file for a gap in the numbers.
- Work out a checksum, for example the sum of all the digits, add it as a last column, and check it in a spreadsheet. Why would a ground station want that?
- Decide what your packet should contain for your own secondary mission, and write the header line for it before you write any code.
- Try sending the same data as a fixed width line instead of comma separated. Which is easier to read by eye, and which is easier for a program to parse?