You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino M0(SAMD21)双SPI接口控制及OZONE2CLICK卡滞求助

Hey there! Let's work through your SAMD21 Arduino M0 SPI and OZONE2CLICK hang issue together.

First, let's clarify the SPI setup on the SAMD21: this chip has two hardware SPI peripherals. The traditional SPI you're referring to is the default SPI (SPI0) peripheral, mapped to standard Arduino pins: SCK=13, MISO=12, MOSI=11. The second SPI (SPI1) uses alternate pins (often labeled with a suffix like MISO1, SCK1 in some libraries). To control the traditional SPI, you just use the standard SPI.h library—no special handling needed, as long as you're wiring to the correct pins and managing the chip select (SS) properly.

Now, let's dig into why your code is hanging at the MQ131.begin() call:

Key Issues in Your Current Code

  1. Late/Uninitialized SPI: The MQ131 library relies on SPI communication, but you're not explicitly initializing SPI before calling MQ131.begin().
  2. Redundant Pin Mode Setup: You're setting pin modes for SS, DcPin, and O2Pin inside the loop()—this should only happen once in setup().
  3. Potential Peripheral Conflicts: You have other devices (RFM69, BME280, INA219) initialized; if any share the SPI bus, you need to ensure only one chip select is active at a time.
  4. Non-Standard Baud Rate: PC_BAUDRATE = 56700 is a non-standard rate, which can cause serial communication issues and make debugging harder.

Fixed Code with Explanations

Here's your code adjusted to resolve these issues:

#include "Arduino.h"
#include <MQ131.h>
#include <SPI.h>

// --- Configuration Tweaks ---
#define PC_BAUDRATE 115200  // Switched to standard baud rate for reliable serial
#define MS_DELAY 0
#define LED_DELAY 100
#define Serial SerialUSB

// SD Card
#define sd_cs_pin 35
float PPMO2;
float PPBO2;
float MGM3O2;
float UGM3O2;

// OZONE2CLICK Pins (confirm these match your wiring!)
const byte pinSS = 2;       // Custom SS pin for Ozone sensor
const byte pinRDY = 12;     // Note: This is the default MISO pin for traditional SPI!
const byte pinSCK = 13;     // Traditional SPI SCK
const byte O2Pin = 10;      // Sensor power pin
#define DcPin 8

// SD Card File
File file;

// LEDs
#define D13_led_pin 42
#define M_led_pin 36

// Local Variables
int idCounter = 1;
bool isBmeOk = true;
bool isSdOk = true;
bool isRadioOk = true;
bool isGpsConnected = true;

// --- Function Definitions ---
void OZONE2CLICKCalibrate () {
 Serial.println("Starting Ozone Sensor Calibration...");
 // Initialize MQ131 with correct parameters
 MQ131.begin(pinSS, pinRDY, O2Pin, LOW_CONCENTRATION, 10000);
 Serial.println("Calibration in progress...");
 MQ131.calibrate();
 Serial.println("Calibration done!");
 Serial.print("R0 = ");
 Serial.print(MQ131.getR0());
 Serial.println(" Ohms");
 Serial.print("Time to heat = ");
 Serial.print(MQ131.getTimeToRead());
 Serial.println(" s");
}

void OZONE2CLICKMeasure () {
 Serial.println("Sampling Ozone...");
 MQ131.sample();
 Serial.print("Concentration O3 (ppm): ");
 PPMO2 = MQ131.getO3(PPM);
 Serial.println(PPMO2);
 Serial.print("Concentration O3 (ppb): ");
 PPBO2 = MQ131.getO3(PPB);
 Serial.println(PPBO2);
 Serial.print("Concentration O3 (mg/m³): ");
 MGM3O2 = MQ131.getO3(MG_M3);
 Serial.println(MGM3O2);
 Serial.print("Concentration O3 (μg/m³): ");
 UGM3O2 = MQ131.getO3(UG_M3);
 Serial.println(UGM3O2);
}

void setup() {
 Serial.begin(PC_BAUDRATE);
 // Wait for serial monitor (uncomment if needed)
 // while(!Serial);
 Serial.println("openCanSat PRO");
 Serial.print("Node ");
 Serial.print(MYNODEID, DEC);
 Serial.println(" ready");

 // --- Initialize Pins Once ---
 pinMode(pinSS, OUTPUT);
 digitalWrite(pinSS, HIGH);  // Keep SS high when not using the sensor
 pinMode(DcPin, OUTPUT);
 digitalWrite(DcPin, HIGH);
 pinMode(O2Pin, OUTPUT);
 digitalWrite(O2Pin, HIGH);
 pinMode(D13_led_pin, OUTPUT);

 // --- Initialize SPI First ---
 SPI.begin();  // Explicitly initialize traditional SPI bus
 SPI.setClockDivider(SPI_CLOCK_DIV4);  // Set a safe clock speed (adjust if needed)

 // --- Initialize Other Peripherals ---
 // BME280
 if (!bme.begin(BME280_ADDRESS_OPEN_CANSAT)) {
 isBmeOk = false;
 Serial.println("Could not find a valid BME280 sensor, check wiring!");
 }
 // INA219
 ina219.begin();
 // GPS
 Wire.beginTransmission(0x42);
 int error = Wire.endTransmission();
 if (error != 0) {
 isGpsConnected = false;
 Serial.println("GPS not connected!");
 }
 gps.begin();
 // RFM69 Radio
 if(!radio.initialize(FREQUENCY, MYNODEID, NETWORKID)) {
 isRadioOk = false;
 Serial.println("RFM69HW initialization failed!");
 } else {
 radio.setFrequency(FREQUENCYSPECIFIC);
 radio.setHighPower(true);
 }
}

void loop() {
 // Wait for sensor to stabilize (10s delay)
 delay(10000);
 
 // Run calibration and measurement
 OZONE2CLICKCalibrate();
 OZONE2CLICKMeasure();
}

Critical Fixes & Notes

  1. Explicit SPI Initialization: Added SPI.begin() and SPI.setClockDivider() in setup() to ensure the traditional SPI bus is ready before communicating with the Ozone sensor.
  2. Pin Mode Cleanup: Moved all pin mode setup to setup()—no need to reconfigure pins every loop iteration.
  3. SS Pin Management: Ensured pinSS is pulled high by default (SPI requires SS to be high when the device is not in use).
  4. Baud Rate Adjustment: Switched to 115200 for reliable serial debugging.
  5. Pin Conflict Check: Your pinRDY is set to 12, which is the default MISO pin for traditional SPI. Double-check that your sensor's RDY pin is actually connected here—if not, adjust this to the correct pin to avoid conflicts.

Additional Troubleshooting Steps

  • Wiring Check: Verify that your OZONE2CLICK is wired to the traditional SPI pins:
    • SCK → Arduino Pin 13
    • MOSI → Arduino Pin 11 (your code didn't define this, but the MQ131 library uses the default MOSI pin)
    • MISO → Arduino Pin 12 (if your sensor uses MISO; if RDY is a separate pin, make sure it's not overlapping)
  • Peripheral Isolation: Temporarily comment out initialization for RFM69, BME280, etc., to rule out bus conflicts.
  • Sensor Power: Ensure the Ozone sensor is getting enough power—some gas sensors require higher current than the Arduino can provide directly.

内容的提问来源于stack exchange,提问作者HASHTAG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 07:13:15