HCPCF8574 - Library for PCF8574 8 bit port expander

Useful guides, libraries, and example sketches to support our Arduino based products.
admin
Site Admin
Posts: 727
Joined: Sun Aug 05, 2012 4:02 pm

HCPCF8574 - Library for PCF8574 8 bit port expander

Post by admin » Mon Jun 05, 2017 3:22 pm

Image




Arduino Library for the PCF8574 I2C to 8-bit digital port expander.
Currently supported products:

PCF8574 I2C to 8-bit digital port expander module (HCMODU0120) available from hobbycomponents.com


This library can be used to configure and control the NXP Semiconductors PCF8571 8-bit digital port expander IC from a standard Arduino development board. Additionally, the library supports the control of multiple devices by creating multiple instances of the library (see HCPCF8574_Multiple_Device_Blink_Example sketch included with library).

You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area.

On Windows:
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/

Linux:
Usually found within the users home area under /Arduino/libraries/


Using the HCPCF8574 library

To use the library just include the HCPCF8574.h header file and then create an instance of the library. E.g:

Code: Select all

#include "HCPCF8574.h"
HCPCF8574 HCPCF8574(I2C_ADD);
Where I2CAdd is the I2C slave address of the device. The PCF8574, and our module via jumper pads, can be set to one of 8 different slave addresses. For the HCMODU0120 the default address is 0x38.


To initialise the library add the following line to the setup() section of your sketch:

Code: Select all

HCPCF8574.init();

The following functions are available with this library:

Code: Select all

HCPCF8574.init();
Initialises the I2C interface and puts the device in to a default state. All IO pins will be set high.


Code: Select all

HCPCF8574.pinMode(Pin, Mode);
Configures a pin to be either an input or an output where:

Pin is the pin number to configure. Valid values for Pin are 0 to 7

Mode is the required pin direction. Valid values for Mode are
OUTPUT (pin is configured to be an output)
INPUT (pin is configured to be an input)

Note: when configured to an input the pin will be driven high and
masked so that is cannot be written to by the pinWrite function.


Code: Select all

HCPCF8574.pinWrite(Pin, State);
Sets the output state of one of the 8 pins where:

Pin is the number of the pin to alter. Valid values for Pin are 0 to 7

State is the state to set the pin to. Valid values for State are
HIGH (pin is pulled high)
LOW (pin is pulled low)

Note: to read the state of a pin it must be configured as an output by pinMode();


Code: Select all

HCPCF8574.pinRead(Pin);
Reads the state on one of the 8 pins where:

Pin is the number of the pin to read. Valid values for Pin are 0 to 7

Returns a Boolean value representing the state of the pin.

Note: to read the state of a pin it must be configured as an input by pinMode();



HCPCF8574.portWrite(Data);

Writes to all 8 pins in one operation where:

Data is an 8 bit binary value representing the state of all 8 pins.


Code: Select all

HCPCF8574::portRead();
Reads the state of all 8 pins in one operation.

Returns and 8 bit value representing the state of a 8 pins.




Image

Code: Select all

/* FILE:    HCPCF8574_Blink_Example.cpp
   DATE:    24/05/17
   VERSION: 0.1
   AUTHOR:  Andrew Davies
   
24/05/17 version 0.1: Original version

This example sketch uses the HCPCF8574 library to toggle one of the PCF8574's digital pins
emulating the standard Arduino 'blink' sketch. This sketch has been written specifically for
the Hobby Components PCF8574 I2C to 8-bit digital port expander (HCMODU0120). To use the 
sketch change the I2C_ADD to match the address of your device (default is 0x38 for HCMODU0120)
and connect and LED (via a current limiting resistor) to the PCF8574's digital pin 0 (marked
D0 on HCMODU0120). You can connect your Arduino to the module as follows:

UNO/NANO........HCMODU0120
VCC.............5V
GND.............GND
A4..............SDA
A5..............SCL

More information about the library can be found in the software section of our support 
forum here:

http://forum.hobbycomponents.com/software


You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/


#include "HCPCF8574.h"    //Include the HCPCF8574 library

#define I2C_ADD 0x38      //I2C address of the PCF8574

HCPCF8574 Port(I2C_ADD);  //Create an instance of the library


void setup() 
{
  Port.init();            //Initiliase the PCF8574 (all pins are set to high)

  Port.pinMode(0, OUTPUT); //Set digital pin 0 to an ouput
}


void loop() 
{
  Port.pinWrite(0, HIGH); //Set digital pin 0 high
  delay(1000);            //Wait 1 second
  Port.pinWrite(0, LOW);  //Set digital pin 0 low
  delay(1000);            //Wait another second
}

Code: Select all

/* FILE:    HCPCF8574_Pin_Read_Example.cpp
   DATE:    24/05/17
   VERSION: 0.1
   AUTHOR:  Andrew Davies
   
24/05/17 version 0.1: Original version

This example sketch uses the HCPCF8574 library to read the state of the PCF8574's 
digital pin 0 once every second. The state of the pin will then be output to the serial 
UART.

This sketch has been written specifically for the Hobby Components PCF8574 
I2C to 8-bit digital port expander (HCMODU0120). To use the sketch change the I2C_ADD to 
match the address of your device (default is 0x38 for HCMODU0120) and either pull digital 
pin 0 high or low to change its state. You can connect your Arduino to the module as follows:

UNO/NANO........HCMODU0120
VCC.............5V
GND.............GND
A4..............SDA
A5..............SCL

More information about the library can be found in the software section of our support 
forum here:

http://forum.hobbycomponents.com/software


You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.
THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/


#include "HCPCF8574.h"    //Include the HCPCF8574 library

#define I2C_ADD 0x38      //I2C address of the PCF8574

HCPCF8574 Port(I2C_ADD);  //Create an instance of the library


void setup() 
{
  Serial.begin(9600);     //Initiliase the Arduino serial library.
  Port.init();            //Initiliase the PCF8574 (all pins are set to high)
  
  Port.pinMode(0, INPUT); //Set digital pin 0 to an input

}


void loop() 
{
  boolean State = Port.pinRead(0);  //Read the state of digital pin 0

  // Output the state to the serial port
  Serial.print("Pin 0 = ");
  if(State)
    Serial.println("HIGH");
  else
    Serial.println("LOW");

  delay(1000);
}

Image
HCPCF8574_V0_2.zip


Diagrams, libraries, and example code are provided as an additional free service by Hobby Components and are not sold as part of this product. We do not provide any guarantees or warranties as to their accuracy or fitness for purpose.

Descriptions and diagrams on this page are copyright Hobby Components Ltd and may not be reproduced without permission.
You do not have the required permissions to view the files attached to this post.

Paul van de Veen
Posts: 8
Joined: Tue Oct 31, 2017 4:32 pm

Re: HCPCF8574 - Library for PCF8574 8 bit port expander

Post by Paul van de Veen » Tue Oct 31, 2017 6:08 pm

The link to the HCPCF8574 library is broken.
Can anyone provide me with this library?

Thanks in advance!
Paul

admin
Site Admin
Posts: 727
Joined: Sun Aug 05, 2012 4:02 pm

Re: HCPCF8574 - Library for PCF8574 8 bit port expander

Post by admin » Tue Oct 31, 2017 7:27 pm

I've just checked it and it seems to download fine for me. Were you logged in when you tried to download it?

jim@felich.com
Posts: 3
Joined: Thu Nov 08, 2018 6:36 pm
Location: Santa Cruz, CA

Re: HCPCF8574 - Library for PCF8574 8 bit port expander

Post by jim@felich.com » Tue Nov 20, 2018 6:58 pm

I just downloaded this library but my AVG anti-virus blocked it saying it was infected with the URL.mal virus.

Please verify that the download is virus free and let me know.

Thanks!

andrew
Site Admin
Posts: 826
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCPCF8574 - Library for PCF8574 8 bit port expander

Post by andrew » Tue Nov 20, 2018 8:06 pm

The library is just a zip file containing a bunch of plain text files. There are no executable files to infect, or to infect your computer. I've just downloaded and checked it myself and everything is as expected. I've also scanned it with Windows defender and with an online virus scanner (virustotal) is its showing as clean.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

powersoft
Posts: 1
Joined: Wed Nov 21, 2018 3:52 pm

Re: HCPCF8574 - Library for PCF8574 8 bit port expander

Post by powersoft » Wed Nov 21, 2018 4:01 pm

Hello,

When I try to download the zip my antivirus is blocking the file. (mal ware)

Please can you take a look.

Cheers,

Powersoft

andrew
Site Admin
Posts: 826
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCPCF8574 - Library for PCF8574 8 bit port expander

Post by andrew » Wed Nov 21, 2018 5:26 pm

I not sure why your AV is blocking it but please see my previous post.

Edit: Just for reference could you let me know what AV software you are using?
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

jim@felich.com
Posts: 3
Joined: Thu Nov 08, 2018 6:36 pm
Location: Santa Cruz, CA

Re: HCPCF8574 - Library for PCF8574 8 bit port expander

Post by jim@felich.com » Thu Nov 22, 2018 10:09 pm

I tried again to download this library, but my AVG anti-virus actually disconnected me from your website.
It says the Forum has a virus called URL:Mal.
Here is the message from AVG.

https://www.dropbox.com/s/s3hw6khot7r2g ... t.jpg?dl=0

Please advise,

andrew
Site Admin
Posts: 826
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCPCF8574 - Library for PCF8574 8 bit port expander

Post by andrew » Fri Nov 23, 2018 9:44 am

I've downloaded and checked the file again and it is defiantly clean. I've also run a scan on both the link for the file and the forum itself and can find no evidence of any infection. Here are some external url scanners that are also showing the file and site to be clean:


Google malware checker:

https://transparencyreport.google.com/s ... 3Fid%3D474

https://transparencyreport.google.com/s ... onents.com


Virustotal:

https://www.virustotal.com/#/url/e34f21 ... /detection

https://www.virustotal.com/#/url/744a61 ... /detection

https://www.virustotal.com/#/file/960e8 ... /detection


It is very likely that your scanner is showing a false positive. My guess is that as the files require a login to download it's probably not able to scan them properly. I've posted a false positive report to AVG but in the mean time if you need the file you can email support [at] hobbycomponents.com and I will email you the file directly.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

andrew
Site Admin
Posts: 826
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCPCF8574 - Library for PCF8574 8 bit port expander

Post by andrew » Fri Nov 23, 2018 4:07 pm

Just received a reply from AVG confirming it is a false positive....

Hello,

Thank you for contacting AVG.
Please accept our apologies for the inconvenience caused. The detection by AVG was incorrect and was removed in a recent AVG update, please wait at least 24 hours.
If the detection persists, please reply to this e-mail and:
1. Attach a screenshot of your virus database version in AVG.
Open AVG ▸ Menu ▸ About
2. Attach the screenshot including the detection dialog, with See details in the bottom-right corner displayed. To create a screenshot, please refer to the following link.
How to create a screenshot
Please do not hesitate to contact AVG at any time with further inquiries.
Thank you for your patience and cooperation, we appreciate it.
Best regards,

AVG Customer Care
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Post Reply