Example Code Card

I'm testing something. can you generate an example code card?
His Holiness, Señior Solveig, here’s an example code card:

Cpp


// Blink an LED on Arduino pin 13
constexpr uint8_t LED_PIN = 13;

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(500);
  digitalWrite(LED_PIN, LOW);
  delay(500);
}
Create Python card
Show markdown card
Test schematic review card
DS2
DS3
DS1
R3
Resistance
10 kΩ
J2
C5
Capacitance
1 µF
R2
Resistance
4.7 kΩ
R10
Resistance
330 kΩ
R8
Resistance
10 kΩ
C9
Capacitance
10 µF
R7
Resistance
100 kΩ
C10
Capacitance
100 nF
C3
Capacitance
4.7 µF
R5
Resistance
10 kΩ
C7
Capacitance
100 nF
R6
Resistance
10 kΩ
R1
Resistance
4.7 kΩ
C1
Capacitance
10 µF
C4
Capacitance
1 µF
C2
Capacitance
4.7 µF
C6
Capacitance
100 nF
R9
Resistance
1 MΩ
J1
R4
Resistance
10 kΩ
C8
Capacitance
100 nF
U1
U3
U5
U2
U4

Refine this doc
Ask about this doc
Firmware Starter — ATmega328PB Solar Compass
Platform & Toolchain
  • MCU: ATmega328PB-AU
  • Framework: Arduino-compatible AVR C++
  • Build system: PlatformIO
  • Key libraries: Wire built in, TM1637Display
Pin Mapping

Table


FunctionMCU PinArduino-style constantConnected ToNotes
I2C SDAPC4 / SDA0A4LIS3MDL SDA, R1 4.7k pull-upMagnetometer data
I2C SCLPC5 / SCL0A5LIS3MDL SCL, R2 4.7k pull-upMagnetometer clock
Display CLKPD55TM1637 CLK, R4 10k pull-upSerial display clock
Display DIOPD66TM1637 DIO, R5 10k pull-upSerial display data
ResetPC6 / RESETn/aR3 10k pull-upHardware reset
PowerVCC, AVCCn/a3.3 V railInternal RC clock assumed
platformio.ini

Ini


[env:atmega328pb]
platform = atmelavr
board = ATmega328PB
framework = arduino
board_build.f_cpu = 8000000L
monitor_speed = 115200
lib_deps =
    avishorp/TM1637@^1.2.0
Complete Firmware Source (src/main.cpp)

Cpp


#include <Arduino.h>
#include <Wire.h>
#include <TM1637Display.h>
#include <math.h>

// Schematic pin mapping
static constexpr uint8_t PIN_TM_CLK = 5;   // U1 PD5 -> U3 CLK
static constexpr uint8_t PIN_TM_DIO = 6;   // U1 PD6 -> U3 DIO
static constexpr uint8_t LIS3MDL_ADDR = 0x1C; // SA1 tied low; use 0x1E if board/library variant reports alternate address

// LIS3MDL registers
static constexpr uint8_t REG_WHO_AM_I = 0x0F;
static constexpr uint8_t REG_CTRL_REG1 = 0x20;
static constexpr uint8_t REG_CTRL_REG2 = 0x21;
static constexpr uint8_t REG_CTRL_REG3 = 0x22;
static constexpr uint8_t REG_CTRL_REG4 = 0x23;
static constexpr uint8_t REG_OUT_X_L = 0x28;

TM1637Display display(PIN_TM_CLK, PIN_TM_DIO);

struct MagRaw {
  int16_t x;
  int16_t y;
  int16_t z;
};

// Calibration values: update after vehicle/dashboard calibration sweep.
float hardIronX = 0.0f;
float hardIronY = 0.0f;
float softScaleX = 1.0f;
float softScaleY = 1.0f;

bool i2cWrite8(uint8_t reg, uint8_t value) {
  Wire.beginTransmission(LIS3MDL_ADDR);
  Wire.write(reg);
  Wire.write(value);
  return Wire.endTransmission() == 0;
}

bool i2cRead(uint8_t reg, uint8_t *buf, size_t len) {
  Wire.beginTransmission(LIS3MDL_ADDR);
  Wire.write(reg);
  if (Wire.endTransmission(false) != 0) return false;
  size_t got = Wire.requestFrom(LIS3MDL_ADDR, (uint8_t)len);
  if (got != len) return false;
  for (size_t i = 0; i < len; ++i) buf[i] = Wire.read();
  return true;
}

bool initLIS3MDL() {
  uint8_t who = 0;
  if (!i2cRead(REG_WHO_AM_I, &who, 1)) return false;

  // Ultra-high-performance XY, 10 Hz ODR: CTRL_REG1 = 0b01110000
  // +/-4 gauss full scale: CTRL_REG2 = 0b00000000
  // Continuous-conversion mode: CTRL_REG3 = 0b00000000
  // Ultra-high-performance Z: CTRL_REG4 = 0b00001100
  bool ok = true;
  ok &= i2cWrite8(REG_CTRL_REG1, 0x70);
  ok &= i2cWrite8(REG_CTRL_REG2, 0x00);
  ok &= i2cWrite8(REG_CTRL_REG3, 0x00);
  ok &= i2cWrite8(REG_CTRL_REG4, 0x0C);
  return ok;
}

bool readMag(MagRaw &m) {
  uint8_t b[6];
  if (!i2cRead(REG_OUT_X_L | 0x80, b, sizeof(b))) return false;
  m.x = (int16_t)((uint16_t)b[1] << 8 | b[0]);
  m.y = (int16_t)((uint16_t)b[3] << 8 | b[2]);
  m.z = (int16_t)((uint16_t)b[5] << 8 | b[4]);
  return true;
}

int headingDegrees(const MagRaw &m) {
  float x = ((float)m.x - hardIronX) * softScaleX;
  float y = ((float)m.y - hardIronY) * softScaleY;
  float heading = atan2f(y, x) * 180.0f / PI;
  if (heading < 0.0f) heading += 360.0f;
  int deg = (int)lroundf(heading);
  if (deg >= 360) deg -= 360;
  return deg;
}

void showHeading(int deg) {
  // Shows 000-359 on the three wired digits. Leading zeroes are useful for compass readout.
  display.showNumberDec(deg, true, 3, 0);
}

void setup() {
  Serial.begin(115200);
  Wire.begin();
  Wire.setClock(100000);

  display.setBrightness(2); // Keep low for solar power budget; raise if needed.
  display.clear();

  if (!initLIS3MDL()) {
    Serial.println(F("LIS3MDL init failed"));
    display.showNumberDec(888, true, 3, 0);
  } else {
    Serial.println(F("Solar compass firmware started"));
  }
}

void loop() {
  static uint32_t lastRead = 0;
  if (millis() - lastRead < 250) return;
  lastRead = millis();

  MagRaw mag;
  if (readMag(mag)) {
    int deg = headingDegrees(mag);
    showHeading(deg);
    Serial.print(F("Heading: "));
    Serial.println(deg);
  } else {
    Serial.println(F("Mag read failed"));
    display.showNumberDec(999, true, 3, 0);
  }
}
Build & Flash
  • Build: pio run -e atmega328pb
  • Flash: use an AVR ISP programmer because the schematic does not include a USB interface; command depends on programmer, e.g. pio run -e atmega328pb -t upload --upload-port .
  • Calibrate after installation by rotating the assembled unit in the vehicle and updating the hard-iron/soft-iron constants.
  • Platform & Toolchain

  • Pin Mapping

  • `platformio.ini`

  • Complete Firmware Source (`src/main.cpp`)

  • Build & Flash

Assets

Assets are files uploaded to this project which can be used in various ways.

FJ Solar Compass Display thumbnail
Solar-powered dashboard compass for an FJ with a 3-digit seven-segment direction display, magnetometer sensing, low-power MCU control, rechargeable battery storage, and solar charging.

Properties

Properties describe core aspects of the project.

Pricing & Availability

Distributor

Qty 1

Arrow

$1.47–$1.57

Digi-Key

$13.32–$13.87

HQonline

$2.20

LCSC

$10.09–$19.40

Mouser

$14.05

TME

$11.73

Verical

$1.56–$17.52

Controls