0

When I connect a piezo (to pin 8), it causes the output of my RTC to go haywire and throw 165 errors into the results.

2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2165/165/165 165:37:4 // Connected piezo
2016/3/25 13:36:4
2165/165/45 13:36:4
2165/165/165 25:36:4
2016/3/25 13:36:4
2165/165/45 13:36:4
2016/3/25 13:36:4
2165/165/45 13:36:4
2165/165/165 165:165:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2165/165/165 37:36:4
2016/3/25 13:36:4
2165/165/27 13:36:4
2016/3/25 13:36:4
2165/165/165 25:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2165/165/165 25:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2165/165/165 25:36:4
2165/165/165 25:36:4
2016/3/25 13:36:4
2016/3/25 13:36:4
2165/165/165 25:36:4

Here's my current layout...

Photo of breadboard

Worth noting:

  • Disconnecting the piezo instantly solves the problem.
  • Connecting to any other pins also caused the issue.

Here is the Fritzing diagram

Fritzing diagram

and schematic

Schematic

Here's the full code...

#include <Adafruit_NeoPixel.h>
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;

#define PIN 6
#define TONE 8
const int buttonPin = 7;

long fadeTime = 1 * 60000L; // X minutes
int colorStops = 256;
int delaySpeed = fadeTime / colorStops;
int notes[] = {262,294,330,349};
int switchState = 1;         // variable for reading the pushbutton status
int timeHour = 13;
int timeMinute = 20;
uint32_t alarmLength = 5 * 60000L; // 5 minutes

Adafruit_NeoPixel strip = Adafruit_NeoPixel(4, PIN, NEO_RGB);

void setup() {
  Serial.begin(57600);

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  if (! rtc.isrunning()) {
    Serial.println("RTC is NOT running!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(__DATE__, __TIME__));
  }

  pinMode(buttonPin, INPUT_PULLUP); // initialize the pushbutton pin as an input

  strip.begin();
  strip.show();
}

void loop() {
  DateTime now = rtc.now(); 

  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.println();

  if (now.hour() == timeHour && now.minute() == timeMinute && now.second() == 00){
    // Fade in light
    for(int i = 1; i<colorStops; i++){
      if (kill() == true){ break; }

      for(int np = 0;np<strip.numPixels(); np++){
        strip.setPixelColor(np, strip.Color(i,i,0));
      }
      strip.show();
      delay(delaySpeed);
    }

    // Play tone after light fully bright
    for( uint32_t tStart = millis();  (millis()-tStart) < alarmLength; ){
      if (kill() == true){ break; }

      for(int np = 0;np<strip.numPixels(); np++){
        strip.setPixelColor(np, strip.Color(random(100,255),random(100,255),random(100,255)));
      }
      strip.show();

      tone(TONE, notes[random(0,3)]);
      delay(100);
    }

    noTone(TONE);

    for(int np = 0;np<strip.numPixels(); np++){
      strip.setPixelColor(np, strip.Color(0,0,0));
    }
    strip.show();
  }
}

bool kill() {
  if (digitalRead(buttonPin) == LOW) {
    noTone(TONE);

    for(int np = 0;np<strip.numPixels(); np++){
      strip.setPixelColor(np, strip.Color(0,0,0));
    }
    strip.show();
    return true;
  }
  else {
    return false;
  }
}
  • Could you please add a schematics diagram of your circuit, would make it easier to understand your connections as opposed to the picture. Could you also please attach your code – evolutionizer Mar 25 '16 at 18:46
  • @evolutionizer Done. – Shpigford Mar 25 '16 at 18:56
  • Is it only in case of connection or also when you drive the piezo? Might it be because the piezo (and rtc) is drawing more current than the Arduino can supply? – Paul Mar 25 '16 at 18:56
  • @Paul I'm noticing only in the case of the connection, without trying to drive the piezo. – Shpigford Mar 25 '16 at 18:57
  • 2
    @Shpigford could you please add pinMode(TONE,OUTPUT); in setup and try again – evolutionizer Mar 25 '16 at 19:04
2

Add

pinMode(TONE,OUTPUT); 

to setup() to fix the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.