I2C Scanner stuck on scanning...

Hi,

I have a lengthy code which I am running off of an Arduino Uno, recently I can't get the code to work but I haven't changed anything. The code is reading two buttons, a toggle switch, a reed switch, two analogue inputs and writing to a bluetooth and SD card readers with a RTC to keep the time. The code stops with "RTC found" and does not enter the void loop, I added the serial.print("entered loop") to check this and it never appears. I thought it was a problem with my RTC so I ran an I2C scanner.

The I2C code gets stuck scanning and my serial print only shows "scanning..." and nothing more. Have I tried to put too much on a Uno board and the board is now crashing? Should I get a Mega or is the problem something else?

My code is attached below.

Any help is appreciated. Thanks

#include <DS3231.h>
// BT Data Logger
// BlueTooth Configuration
/* Include the software serial port library */
#include <SoftwareSerial.h>
//Include SD card library
#include <SD.h>
//Include RTC library
#include <RTClib.h>
#include <Wire.h>


#define LOG_INTERVAL  1000 // mills between entries (reduce to take more/faster data)
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()
#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()
/* to communicate with the Bluetooth module's TXD pin */
#define BT_SERIAL_TX 6
/* to communicate with the Bluetooth module's RXD pin */
#define BT_SERIAL_RX 5
/* Initialise the software serial port */
SoftwareSerial BluetoothSerial(BT_SERIAL_TX, BT_SERIAL_RX);
//SD card file
File logfile;
RTC_DS3231 RTC;
//DS3231  RTC(SDA, SCL);

const int INbuttonPin = 15;     // the number of the in pushbutton pin
const int OUTbuttonPin = 14;     // the number of the out pushbutton pin
const int togglePin = 4;
int toggleState;
int proximity;
unsigned int Occupancy = 0;
const int ReedPin = 7;
int ReedState = 0;
int GrPin = 8;
int BlPin = 3;
int YlPin = 2;
int RdPin = 9;

char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
int analogPin1 = A4;     // potentiometer wiper (middle terminal) connected to analog pin 3
int val1 = 0;           // variable to store the value read
int analogPin2 = A3;     // potentiometer wiper (middle terminal) connected to analog pin 3
int val2 = 0;           // variable to store the value read

void setup() {
  /* Set the baud rate for the software serial port */
  pinMode(INbuttonPin, INPUT);
  pinMode(OUTbuttonPin, INPUT);
  pinMode(togglePin, INPUT);
  pinMode(ReedPin, INPUT);
  pinMode(GrPin, OUTPUT);
  pinMode(BlPin, OUTPUT);
  pinMode(YlPin, OUTPUT);
  pinMode(RdPin, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(14, INPUT);
  pinMode(15, INPUT);
  pinMode(17, INPUT);
  pinMode(18,OUTPUT);
  pinMode(11, INPUT);
  pinMode(16,OUTPUT);
  Serial.begin(9600);
  BluetoothSerial.begin(9600); // Initialise BlueTooth
  delay(3000);
  Serial.print("Initializing SD card...");
  BluetoothSerial.print("Starting ...");
  BluetoothSerial.print("Initializing SD card...");
  BluetoothSerial.print("\r\n");

  if (!SD.begin(10)) {
    Serial.println("initialization failed!");
    BluetoothSerial.print("Initialization failed");
    BluetoothSerial.print("\r\n");
    digitalWrite(18, HIGH);
    
   // return;
  }
  else {
    Serial.println("initialization done.");
    BluetoothSerial.print("SD Card initilizatin done");
    BluetoothSerial.print("\r\n");
  }

  if (! RTC.begin()) {
    Serial.println("Couldn't find RTC");
    BluetoothSerial.print("Counldn't find RTC");
    BluetoothSerial.print("\r\n");
    while (1);
  }  
  if(RTC.begin()){
  Serial.println("RTC found");  
  RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
  
  if (RTC.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
   // RTC.adjust(DateTime(2017, 7, 20, 5, 1, 1));
  }

  if (! logfile) {
    Serial.print("couldnt create file");
    BluetoothSerial.print("counldn't create SD file");
    BluetoothSerial.print("\r\n");
  }

else{
  char filename[] = "Data.CSV";
  File logfile = SD.open(filename, FILE_WRITE);
  #define LOGFILE "Data.CSV"
  Serial.print("Logging to: ");
  Serial.println(filename);
  BluetoothSerial.print("Logging to: ");
  BluetoothSerial.println(filename);
  BluetoothSerial.print("\r\n");

  logfile.println("datetime,occupancy,light,door,Solar 1,Solar 2");
  Serial.print("end of Setup");
}
}
void loop() {
  // Take readings
  int INreading = digitalRead(INbuttonPin);
  int OUTreading = digitalRead(OUTbuttonPin);
  int toggleState = digitalRead(togglePin);
  ReedState = digitalRead(ReedPin);
  int proximity = digitalRead(ReedPin); // Read the state of the switch
  Serial.print("enter loop");
  
      // only toggle the LED if the new button state is HIGH
      if (INreading == HIGH) {
        Occupancy = Occupancy + 1;
        Serial.println("Occupancy=");
        Serial.print(Occupancy);
        BluetoothSerial.println("Occupancy=");
        BluetoothSerial.print(Occupancy);
        BluetoothSerial.print("\r\n");
      }

      // only toggle the LED if the new button state is HIGH
      if (OUTreading == HIGH && Occupancy > 0) {
        Occupancy = Occupancy - 1;
        Serial.println("Occupancy=");
        Serial.print(Occupancy);
        BluetoothSerial.print("Occupancy: ");
        BluetoothSerial.print(Occupancy);
        BluetoothSerial.print("\r\n");
      }
      else {
        Occupancy == 0;
      }

BluetoothSerial.print(Occupancy);
BluetoothSerial.print("\r\n");

  if (toggleState == HIGH) {
    Serial.println("Lights ON");
    BluetoothSerial.print("Lights ON");
    BluetoothSerial.print("\r\n");
  }
  else {
    Serial.println("Lights OFF");
    BluetoothSerial.print("Lights OFF");
    BluetoothSerial.print("\r\n");
  }

if (proximity == HIGH) {
  Serial.println("Door Open");
  BluetoothSerial.print("Door Open");
  BluetoothSerial.print("\r\n");
}
else {
  Serial.println("Door Closed");
  BluetoothSerial.print("Door Closed");
  BluetoothSerial.print("\r\n");
}

if (Occupancy == 0) {
  digitalWrite(RdPin, LOW);     // All LEDS off
  digitalWrite(BlPin, LOW);
  digitalWrite(YlPin, LOW);
  digitalWrite(GrPin, LOW);
}
if (Occupancy == 1) {
  digitalWrite(GrPin, HIGH);     // sets the Green LED on
  digitalWrite(BlPin, LOW);
  digitalWrite(YlPin, LOW);
  digitalWrite(RdPin, LOW);
}
if (Occupancy == 2) {
  digitalWrite(BlPin, HIGH);     // sets the Blue LED on
  digitalWrite(GrPin, HIGH);
  digitalWrite(YlPin, LOW);
  digitalWrite(RdPin, LOW);
}
if (Occupancy == 3) {
  digitalWrite(YlPin, HIGH);     // sets the Yellow LED on
  digitalWrite(BlPin, HIGH);
  digitalWrite(GrPin, HIGH);
  digitalWrite(RdPin, LOW);
}
if (Occupancy == 4) {
  digitalWrite(RdPin, HIGH);     // sets the Red LED on
  digitalWrite(BlPin, HIGH);
  digitalWrite(YlPin, HIGH);
  digitalWrite(GrPin, HIGH);
}

int sensorValue = analogRead(analogPin1);    // read the input pin
float voltage= sensorValue * (5.0 / 1023.0);
Serial.println(voltage);             // debug value
BluetoothSerial.print("PV1=");
BluetoothSerial.print(voltage);
BluetoothSerial.print("\r\n");

int sensorValue2 = analogRead(analogPin2);    // read the input pin
float voltage2= sensorValue2 * (5.0 / 1023.0);
Serial.println(voltage2);             // debug value
BluetoothSerial.print("PV2=");
BluetoothSerial.print(voltage2);
BluetoothSerial.print("\r\n");

DateTime now = RTC.now();
delay((LOG_INTERVAL - 1) - (millis() % LOG_INTERVAL));

uint32_t m = millis();
//logfile.print(m);           // milliseconds since start
//logfile.print(", ");

Serial.print('"');
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.print('"');
Serial.println(" ");

logfile.println('"');
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print('"');
logfile.print(",");
logfile.print(Occupancy);
logfile.print(",");
logfile.print(toggleState);
logfile.print(",");
logfile.print(proximity);
logfile.print(",");
logfile.println(" ");
logfile.print(voltage);
logfile.print(",");
logfile.println(" ");
logfile.print(voltage2);
logfile.print(",");
logfile.println(" ");

if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
logfile.flush();

}

Yes, you can break a program by using to much memory but that only that program. Uploading a good problem (like the scanner) should fix it again.

I think you have a connection problem with the RTC. Try disconnecting everything from I2C and run the scanner again.

Is the RTC the only thing on I2C?

Yes the RTC is the only component on the I2C. When I unplug the RTC the scanner still gets stuck on "scanning.."

Also I am getting a warning when I upload the code saying "Low memory available, stability problems may occur"

Then disconnect everything else except usb. Still stuck?

Please post (in code-tags) the code you use for the scanner.

When I unplug everything it reports no device found

// --------------------------------------
// i2c_scanner
//
// Version 1
//    This program (or code that looks like it)
//    can be found in many places.
//    For example on the Arduino.cc forum.
//    The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
//     Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26  2013
//    V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
//    by Arduino.cc user Krodal.
//    Changes by louarnold removed.
//    Scanning addresses changed from 0...127 to 1...119,
//    according to the i2c scanner by Nick Gammon
//    http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
//    As version 4, but address scans now to 127.
//    A sensor seems to use address 120.
// Version 6, November 27, 2015.
//    Added waiting for the Leonardo serial communication.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
 
#include <Wire.h>
 
 
void setup()
{
  Wire.begin();
 
  Serial.begin(9600);
  while (!Serial);             // Leonardo: wait for serial monitor
  Serial.println("\nI2C Scanner");
}
 
 
void loop()
{
  byte error, address;
  int nDevices;
 
  Serial.println("Scanning...");
 
  nDevices = 0;
  for(address = 1; address < 127; address++ )
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
 
    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
 
      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown error at address 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");
 
  delay(5000);           // wait 5 seconds for next scan
}

When I unplug the RTC the scanner still gets stuck on "scanning.."

When I unplug everything it reports no device found

int analogPin1 = A4;

A4 and A5 are SDA/SCL on the UNO. They can't be used for analog inputs when using i2c.

Missed that one! Great catch @cattledog! (No pun intended...)

Indeed, remove the pot from A4 and connect it to another pin. That's what driving your pin HIGH/LOW and keeping the scanner (and other I2C stuff) waiting.