Ciao a tutti,
ho questo problema. Sto cercando di fare un datalogger con arduino pro-mini 3,3V, scheda microsd e sensore MLX90614. Il sensore è collegato tramite SCL e SDA mentre la scheda microsd tramite il protocollo SPI classico (pin 10 per SS). il VCC di entrambi i sensori è sono collegati al 3,3V dell'arduino. Il negativo del sensore e microsd è invece collegato al GND di arduino tramite un transistor NPN con resistenza di 220ohm sulla base....il tutto per ridurre al minimo il consumo di corrente.
Se testo i singoli componenti, tutto funziona, mentre quando metto tutto insieme arduino si resetta e non procede oltre.
Il codice per il singolo sensore è:
#include <Wire.h>
#include <Adafruit_MLX90614.h>
#include "LowPower.h"
#define pwr 8
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
float a, o;
void setup() {
pinMode (pwr, OUTPUT);
digitalWrite(pwr, HIGH);
Serial.begin(19200);
Serial.println("Adafruit MLX90614 test");
mlx.begin();
}
void loop() {
digitalWrite(pwr, HIGH);
delay(500);
a = mlx.readAmbientTempC();
o = mlx.readObjectTempC();
Serial.print(a); Serial.print('\t'); Serial.println(o);
delay(8000);
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
digitalWrite(pwr, LOW);
delay(500);
a = mlx.readAmbientTempC();
o = mlx.readObjectTempC();
Serial.print(a); Serial.print('\t'); Serial.println(o);
delay(8000);
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
Quello per la singola modulo della scheda microsd è:
#include <Wire.h>
#include <LowPower.h>
#include <avr/power.h>
#include <SPI.h>
#include <SdFat.h>
#define pwpin 8
const int chipSelect = 10;
char filename[15] = "log.txt"; // Set filename Format: "12345678.123". Cannot be more than 8 characters in length, contain spaces or begin with a number
int a;
byte keep_SPCR;
SdFat sd; // declare object for SdFat class
SdFile file; // declare object for SdFile class
void turnOffSPI() {
delay(6);
SPCR = 0; // disable SPI
power_spi_disable(); // disable SPI clock
DDRB &= ~((1 << DDB5) | (1 << DDB4) | (1 << DDB3) | (1 << DDB2)); // set All SPI pins to INPUT
PORTB |= ((1 << DDB5) | (1 << DDB4) | (1 << DDB3) | (1 << DDB2)); // set ALL SPI pins HIGH (~30k pullup)
// Note: you must disconnect the LED on pin 13 or you’ll bleed current through the limit resistor
LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF); // wait 1 sec for internal SDcard housekeeping
delay(6);
}
void turnOnSPI() { // some cards will fail on power-up unless SS is pulled up ( & D0/MISO as well? )
delay(6); // let the card settle
DDRB = DDRB | (1 << DDB5) | (1 << DDB3) | (1 << DDB2); // set SCLK(D13), MOSI(D11) & SS(D10) as OUTPUT
// Note: | is an OR operation so the other pins stay as they were. (MISO stays as INPUT)
PORTB = PORTB & ~(1 << DDB5); // disable pin 13 SCLK pull-up – leave pull-up in place on the other 3 lines
power_spi_enable(); // enable the SPI clock
SPCR = keep_SPCR; // enable SPI peripheral
delay(6);
if (!sd.begin(chipSelect, SPI_FULL_SPEED)) { // initialize SD card on the SPI bus - very important
Serial.println("Card failed");
}
delay(6);
}
void setup() {
Wire.begin();
Serial.begin(19200);
pinMode (pwpin, OUTPUT);
digitalWrite(pwpin, HIGH);
delay(6);
if (!sd.begin(chipSelect, SPI_FULL_SPEED)) { // initialize SD card on the SPI bus - very important
Serial.println("Card failed");
}
else {
delay(20);
file.open(filename, O_CREAT | O_APPEND | O_WRITE); // open file in write mode and append data to the end of file
delay(20);
file.println("test");
file.close();
delay(20);
Serial.println("card initialized");
delay(10);
}
keep_SPCR = SPCR;
}
void loop() {
turnOffSPI();
pinMode(pwpin, OUTPUT); digitalWrite(pwpin, LOW);
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); //sleep mode here
pinMode(pwpin, OUTPUT); digitalWrite(pwpin, HIGH);
turnOnSPI();
delay(8000);
a = analogRead(A0);
LogToSD();
}
void LogToSD() {
file.open(filename, O_WRITE | O_AT_END); // open file in write mode
delay(20);
file.println(a);
file.close();
delay(50);
Serial.print(a);
Serial.println(" values written on SD");
delay(1000);
}
come vedete per ridurre al minimo i consumi ho aggiunto turnOFFSPI e successivamente turnONSPI.
Quando unisco tutto
#include <Wire.h>
#include "LowPower.h"
#include <avr/power.h>
#include <SPI.h>
#include <SdFat.h>
#include <Adafruit_MLX90614.h>
#define pwr 8
const int chipSelect = 10;
char filename[15] = "log.txt"; // Set filename Format: "12345678.123". Cannot be more than 8 characters in length, contain spaces or begin with a number
Adafruit_MLX90614 mlx = Adafruit_MLX90614();
float a, o;
SdFat sd; // declare object for SdFat class
SdFile file; // declare object for SdFile class
void setup() {
Wire.begin();
Serial.begin(19200);
mlx.begin();
pinMode (pwr, OUTPUT);
digitalWrite(pwr, HIGH);
delay(6);
if (!sd.begin(chipSelect, SPI_FULL_SPEED)) { // initialize SD card on the SPI bus - very important
Serial.println("Card failed");
}
else {
delay(20);
file.open(filename, O_CREAT | O_APPEND | O_WRITE); // open file in write mode and append data to the end of file
delay(20);
file.println("test");
file.close();
delay(20);
Serial.println("card initialized");
delay(10);
}
}
void loop() {
turnOffSPI();
pinMode(pwr, OUTPUT); digitalWrite(pwr, LOW);
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); //sleep mode here
pinMode(pwr, OUTPUT); digitalWrite(pwr, HIGH);
turnOnSPI();
delay(5000);
a = mlx.readAmbientTempC();
o = mlx.readObjectTempC();
if (!sd.begin(chipSelect, SPI_FULL_SPEED)) { // initialize SD card on the SPI bus - very important
Serial.println("Card failed");
}
delay(6);
LogToSD();
}
void turnOffSPI() {
delay(6);
SPCR = 0; // disable SPI
power_spi_disable(); // disable SPI clock
DDRB &= ~((1 << DDB5) | (1 << DDB4) | (1 << DDB3) | (1 << DDB2)); // set All SPI pins to INPUT
PORTB |= ((1 << DDB5) | (1 << DDB4) | (1 << DDB3) | (1 << DDB2)); // set ALL SPI pins HIGH (~30k pullup)
// Note: you must disconnect the LED on pin 13 or you’ll bleed current through the limit resistor
LowPower.powerDown(SLEEP_1S, ADC_OFF, BOD_OFF); // wait 1 sec for internal SDcard housekeeping
delay(6);
}
void turnOnSPI() { // some cards will fail on power-up unless SS is pulled up ( & D0/MISO as well? )
delay(6); // let the card settle
DDRB = DDRB | (1 << DDB5) | (1 << DDB3) | (1 << DDB2); // set SCLK(D13), MOSI(D11) & SS(D10) as OUTPUT
// Note: | is an OR operation so the other pins stay as they were. (MISO stays as INPUT)
PORTB = PORTB & ~(1 << DDB5); // disable pin 13 SCLK pull-up – leave pull-up in place on the other 3 lines
power_spi_enable(); // enable the SPI clock
SPCR = keep_SPCR; // enable SPI peripheral
delay(6);
if (!sd.begin(chipSelect, SPI_FULL_SPEED)) { // initialize SD card on the SPI bus - very important
Serial.println("Card failed");
}
delay(6);
}
void LogToSD() {
file.open(filename, O_WRITE | O_AT_END); // open file in write mode
delay(20);
file.print(a); file.print('\t'); file.println(o);
file.close();
delay(50);
Serial.print(a); Serial.print('\t'); Serial.println(o);
Serial.println(" values written on SD");
delay(1000);
}
il codice quando esce dallo sleep non procede ma si resetta, ritornando ad inizializzare la microsd.
Avete idea del perchè? e come potrei risolvere?
HELP