i2c RTC and pcf8574

I have a clock program that has a TFT display and I have recently added RTC to it and a port expander so that I can have some physical keys.

I was having a lot of trouble with the RTC giving back spurious data with date time of 165/165/2165 165:165:85 instead of 8/18/2017 16:0:50

I read one article which indicated that this might be due to a voltage issue, so through experimentation I found that I could get it all working nicely by pulling the power on the PCF8574.

Currently my RTC seems to be working correctly only when the power is removed from the pcf8574 , stick the power on to it and the RTC goes screwey, remove the power and it works great.

So if the pcf8574 is working ok when it does not have power applied to the vcc pin, I guess its either getting power from the input pins as they are connected via resistors to power or via the i2c.

I have checked my circuit and I have 4.7k pull up resistors on the SDA and SCL lines and 2k pull up resistors on the switches.

I would like to be able to understand why the circuit works with the vcc removed from the pcf8574, and is it reasonable to just except that its working and I needn't worry about the vcc for pcf8574.

tft_rtc_pc8574.jpg

how much power do you draw in total?

That's probably a very good question, unfortunately I have no idea, nor any of how to work it out.

I can say that it makes no difference whether the uno is connected via USB or with a power pack or with both, my power pack says 9v 600ma.


Get a multimeter!

Do you have a 0.1uF bypass cap on the pcf chip?

I have a multimeter, but don't know how to measure the power draw.

No don't have a bypass cap, the things I found on the web just indicated a need for the pull down resistor, however I note that there are some little doo hickeys on the board between the chip and the input pins, so perhaps they already exist. I tried using my multimeter to find their value, but, the results I got did not gell with my expectation, so am probably doing it wrong.

Google for a tutorial on using a multimeter.

I wonder if the problem could be too much pull-up on the sda/scl lines? It could well be that both the rtc module and the pcf module have pull-ups built in. The Uno uses its internal pull-ups also. Maybe when you pull the power to the pcf module, it's built-in pull-ups can no longer function, and that's why the circuit starts to work.

Try removing the 4K7 pull-ups from your breadboard, or replace them with 10K, and give power back to the pcf module.

10k resistors in place, both i2c devices have power, no smoke , seems to be working .

You say it gives power back to the pcf, not sure I understand that.

The 4k7 resistors (now 10k) connect between the SDA SDL lines to the two devices and the power bus on my breadboard that is fed by the 5v power on the UNO. So if power is going back to somewhere wouldn't it be back to the UNO.

Thanks

I meant re-connect 5V to the pcf module. I thought you said earlier that you had disconnected that. Its probably not a good idea to let it power itself "parasitically" through pins not designed for that purpose.

Thankyou

I now have 2 i2c devices each are connected to sda and slc with a 4k7 pull up resistor

I was having issues with switches not behaving correctly when both devices were powered

I tried the 10k resistors on sda and slc and that worked for a short while but still had issues with switches so went back to 4k7.

So over a couple of days I found that if I pulled the power on the port expander then the RTC worked fine on boot up and my program would then load ok reading the RTC, but then the keys didn't work, so if I then stuck the power back in for the port expander the keys worked, but the RTC then didn't.

So after some dicking about I stuck a 10k resistor on the power line to the port expander. All my problems went away. The RTC works, and, the all the keys work.

So why put the 10k resistor on the power line to the port expander power, well it was working a bit, but 5 volts seemed to stuff everything up so I figured maybe less volts would be better, and it works now and there has been no smoke so maybe I was lucky and it was the right thing to do.

No, putting 10K on the Vcc pin of a chip isn't ever the right thing to do. You have just been lucky. If you make changes to the circuit, your luck could run out at any moment and it will stop working again.

If you want help figuring out what the real problem is, post a schematic diagram, your code, and a few well lit, well focussed pics would not hurt either.

schematic and pics and code

7seg4.ino (27.7 KB)




Please read the forum guidelines in the stick post to find out how to post your code. I can't open a .ino on my phone.

As it happens I had read http://forum.arduino.cc/index.php/topic,148850.0.html before posting the previous because I could not remember what the code things were and I posted with my code inside of

little square brackets with code doo hickeys

(oops cocked that up on the first attempt, slash in the wrong direction, it's a bugger having to wait 5 minutes to do an edit)  Should have previewed, but I didn't.

Once I didn't do that, I had the temerity to stick 20 lines of code in without the code keys and boy did my helper that time go crook, it is obviously a cardinal sin and I have learned that lesson, no more cardinal sins for this fella ( too old ).

BUT
there was too much text, so, I thought oh well attach another file, as the file is an Arduino file which has a extention of ino it seemed reasonable to me that I attach the ino file.

AND
there is nothing that I can see ( even did a search) in http://forum.arduino.cc/index.php/topic,148850.0.html that says don't attach inos

BUT
live and learn, here it is as a text file

original file removed smaller test file coming

thanks

That is quite a lot of code! I can see now why you could not post it with code tags.

Here's what I suggest. Start a new empty get sketch. Copy the bare minimum of code lines from your current sketch to the new sketch to test the rtc by reading the current time and printing it out to serial monitor. Then bring in a few more lines to test reading the buttons from the pcf chip. Let's see if the two work well together in a simple sketch, without the crazy 10K on the pcf's Vcc pin. If it does work, then we need to look again at the larger sketch, because some kind of software fault must be the cause. If the short sketch still does not work, then you have something to post on code tags so that all the experts here can read it and be convinced this really is a hardware problem.

I should have thought of creating a smaller test program myself. I was sure someone would say that it was totally obvious that I had a resistor wrong or a wire in the wrong spot or my tounge stuck out incorrectly, couldn't possibly be my code.

So anyway created a test program, but kept the TFT on my first downsize of code as I wanted to see whether all the components worked together with minimal code..

So

The switches (and PCF8574) work perfectly with the test program and the TFT works the RTC is read correctly, so it seems that its probably my software. Also works perfectly with my crappily soldered 5 pin keyboard.

Loaded up my large program and that needs the resistor to work correctly. grrrr

So I guess I am 99.5% convinced its not hardware, and I will now add my complex code in bit by bit till I break it again.

Here's the test code for your interest

I will let you know how I get on

thanks

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <UTFTGLUE.h>
#include <TimedAction.h>
#include <TouchScreen.h>
#include <PCF8574.h>
#include <Wire.h>
#include "RTClib.h"
 

int count = 0;
int count2 = 0;
int statePin = LOW;
int piezoPin = 10;

/* *********************************** tft display ******************************************************* */

MCUFRIEND_kbv tft;

UTFTGLUE myGLCD(0x9488, A2, A1, A3, A4, A0);

#if defined(__SAM3X8E__)
#und ef __FlashStringHelper::F(string_literal)
#define F(string_literal) string_literal
#endif

// most mcufriend shields use these pins and Portrait mode:
uint8_t YP = A1;  // must be an analog pin, use "An" notation!
uint8_t XM = A2;  // must be an analog pin, use "An" notation!
uint8_t YM = 7;   // can be a digital pin
uint8_t XP = 6;   // can be a digital pin
uint8_t SwapXY = 1;  // this was orignally 0 but drawing was inversed

 // screen 1 uses full screen   touch is scewed on theat screen
 //uint16_t TS_LEFT = 920;
 //uint16_t TS_RT = 150;
 //uint16_t TS_TOP = 940;
 //uint16_t TS_BOT = 120;

 // screen 2  made no difference whatsoever
uint16_t TS_LEFT = 946;
uint16_t TS_RT = 211;
uint16_t TS_TOP = 287;
uint16_t TS_BOT = 750;


char *name = "Unknown controller";

// For better pressure precision, we need to know the resistance
// between X+ and X- Use any multimeter to read it
// For the one we're using, its 300 ohms across the X plate
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
TSPoint tp;

//#define MINPRESSURE 20
//#define MAXPRESSURE 1000

#define SWAP(a, b) {uint16_t tmp = a; a = b; b = tmp;}

int16_t BOXSIZE;
int16_t PENRADIUS = 3;
uint16_t identifier, oldcolor, currentcolor;
uint8_t Orientation = 1;    //0 is PORTRAIT

 // Assign human-readable names to some common 16-bit color values:
#define BLACK   0x0000
#define BLUE    0x001F
#define RED     0xF800
#define GREEN   0x07E0
#define CYAN    0x07FF
#define MAGENTA 0xF81F
#define YELLOW  0xFFE0
#define WHITE   0xFFFF
 // Declare which fonts we will be using
#if !defined(SmallFont)
extern uint8_t SmallFont[];    //.kbv GLUE defines as GFXFont ref
#endif
#if !defined(LargeFont)
extern uint8_t LargeFont[];    //.kbv GLUE defines as GFXFont ref

#endif

/* ***********************************  RTC ******************************************************* */

RTC_DS1307 RTC;

/* *********************************    pcf 8574  ****************************************************************** */

#define LED_ON 0
#define LED_OFF 1

PCF8574 PCF_38(0x38);  // add switches to lines  (used as input)
PCF8574 PCF_39(0x39);  // add leds to lines      (used as output)

uint8_t expander = B00100000;

TimedAction timer = TimedAction(100, control_function);
/* *************************************************** */
void setup()
{
 Serial.begin(9600);

 PCF8574::PCF8574(expander);

 get_rtc_time();


 uint16_t tmp;

 tft.begin(9600);

 tft.reset();
 identifier = tft.readID();

 randomSeed(analogRead(5));   //.kbv Due does not like A0
 pinMode(A0, OUTPUT);       //.kbv mcufriend have RD on A0
 digitalWrite(A0, HIGH);

 // Setup the LCD

 name = "ILI9488";

 switch (Orientation) {      // adjust for different aspects
 case 0:   break;        //no change,  calibrated for PORTRAIT
 case 1:   tmp = TS_LEFT, TS_LEFT = TS_BOT, TS_BOT = TS_RT, TS_RT = TS_TOP, TS_TOP = tmp;  break;
 case 2:   SWAP(TS_LEFT, TS_RT);  SWAP(TS_TOP, TS_BOT); break;
 case 3:   tmp = TS_LEFT, TS_LEFT = TS_TOP, TS_TOP = TS_RT, TS_RT = TS_BOT, TS_BOT = tmp;  break;
 }

 // ts = TouchScreen(XP, YP, XM, YM, 300);     //call the constructor AGAIN with new values.

 tft.begin(identifier);

 tft.setRotation(Orientation);

 myGLCD.InitLCD();
 myGLCD.setFont(LargeFont);

 myGLCD.clrScr();
 

 refresh_tft_display();

}
/**************************************/
void loop() {
 timer.check();
}

/**************************************/

void control_function()
{
 int val = 0;

 for (int sw = 0; sw < 5; sw++)
 {

 val = PCF_38.read(sw);  // get the value for pin i
 // Serial.print(x);
 // Serial.print("val");
 // Serial.print(val);
 // Serial.print(" ");

 if (val == 0) // key is pressed 
 {

 myGLCD.setColor(0, 0, 0);
 myGLCD.fillRect(0, 0, 480, 120);

 tft.setTextColor(GREEN);
 tft.setTextSize(3);
 tft.setCursor(100, 25);
 tft.print("key ");
 tft.print(String(sw));
 tft.print(" was pressed");
 soundAlarm();
 

 }
 }
 refresh_tft_display();

}
/* *************************************************** */
void refresh_tft_display()
{
 count++;
 count2++;


 if (count == 10)
 {
 count = 0;
 myGLCD.setColor(0, 0, 0);
 myGLCD.fillRect(0, 105, 480, 320);
 
 tft.setTextSize(9);
 tft.setTextColor(WHITE);
 tft.setCursor(200, 125);

 tft.print(String(count2/10));
 }




}
/* *************************************************** */
void get_rtc_time()
{


 Wire.begin();
 RTC.begin();

 if (!RTC.isrunning()) {
 Serial.println("RTC is NOT running!");
 }
 else
 {
 Serial.println("clock is running");
 DateTime now = RTC.now();
 Serial.print(now.month(), DEC);
 Serial.print('/');
 Serial.print(now.day(), DEC);
 Serial.print('/');
 Serial.print(now.year(), DEC);
 Serial.print(' ');
 Serial.print(now.hour(), DEC);
 Serial.print(':');
 Serial.print(now.minute(), DEC);
 Serial.print(':');
 Serial.print(now.second(), DEC);
 Serial.println();
 delay(1000);
 Wire.end();
 }
}
void soundAlarm() {
 for (int j = 0; j < 10; j++)
 {
 float alarmFrequency = 100; // The value for the alarm tone in Hz
 float period = (1.0 / alarmFrequency) * 10000000;
 long beepDuration = 5000; // the time in microseconds (0.25 seconds)
 long elapsedTime = 0;
 


 while (elapsedTime < beepDuration) {
 digitalWrite(piezoPin, HIGH);
 delayMicroseconds(period / 4);
 digitalWrite(piezoPin, LOW);
 delayMicroseconds(period / 4);
 elapsedTime += (period);
 }
 digitalWrite(piezoPin, LOW);
 delayMicroseconds(beepDuration * 4);
 }
}

AND attached is a pic of a new breadboard that's not so much spaghetti

All I see in your last response is a small box with a square in it at the top left and 6" of empty space.

There was a previous post that had two of those blank areas. Perhaps an embedded graphic that doesn't work???

No text at all.

Time passes and things change. So some hours after it was working nicely, I come back to find the circuit misbehaving, ultimately I strip everything back to an UNO ( or duemilanove) and RTC and I can't get the RTC to work, tried multiple different sets of wires and cursing and swearing, RTC appears cactus, oops. ebay is sending some more!

gc_malcolm:
All I see in your last response is a small box with a square in it at the top left and 6" of empty space.

There was a previous post that had two of those blank areas. Perhaps an embedded graphic that doesn't work???

No text at all.

Both times, I was attaching your images to a blank post. Is the quickest way for me to see them on my phone, and helps anyone else reading the thread. The images were visible immediately after I posted them, but disappeared later. I wonder if a new anti-spam measure has been introduced that removes images if there is no accompanying text. I have re-added the image in the blank post above. The image is visible again. Let's see what happens.

Do the rtc modules arrive with pins already soldered? If not, consider soldering them in such a way that you can plug the rtc directly into the breadboard. The fewer wires the better when prototyping!

I can see the images now.

On the two RTC I had the pins came off and I soldered them on , I am afraid that I may have cocked up the power on both of them and fried them - I keep getting blue and brown confused - doh - your thought of plugging directly into the bread board is interesting, I hadn't thought of that, but you still have to move the connections about with wires on the breadboard, but I guess they can be stiff wires and maybe are less troublesome ( as long as there not blue and brown ), should have new RTC in a week and will see. thanks