Showing posts with label I2C. Show all posts
Showing posts with label I2C. Show all posts

2016/05/09

Arduino Sketch Writing MakerSwitch Tail Settings to ATMEL AT24C02 I2C EEPROM


Wiring

1. AT24C02.P01 - GND
2. AT24C02.P02 - GND
3. AT24C02.P03 - GND
4. AT24C02.P04 - GND
5. AT24C02.P05 - ArduinoUno.A4
6. AT24C02.P06 - ArduinoUno.A5
7. AT24C02.P07 - GND
8. AT24C02.P08 - 5V

Sketch

// Modified by Befun Hung on May 9, 2016
//Write setting of MakerSwitch to ATMEL AT24C02
// Address 0-7 contains ID number, address 8 contains address storing switch status value '0' or '1'
// Modified by Befun Hung on May 2, 2016 
// For writing string to ATMEL AT24C02 I2C EEPROM from address 0 once and display all values in the EEPROM on serial monitor once
// The ATMEL AT24C02 I2C EEPROM with 256 bytes (2K bits) need only one byte for addressing is suitable for storing short text message such as id and ip configuration
//
// ** Original comment in simplified Chinese, the original sketch write in EEPROM numbers equal to it's address, read the values and display on serial monitor repeatedly 
//直接给你个24c02简单的程序吧  不用附加库  别的带附加库也可以
//byte 换为int也行, 多数据,大于255的数据自己for循环存吧,double float论坛有教程
#include <Wire.h>

#define colPerLine 8
// define idLen according the length of id value needed
#define idLen 8
char id[idLen+1] = "25610249";


int i = 0;
int val; 

void Writebyte(byte DeviceAddress, byte DataAddress, byte Data)
{
  int rData = Data;
  Wire.beginTransmission(DeviceAddress);
  Wire.write(DataAddress);
  Wire.write(rData);
  Wire.endTransmission();
  delay(10);
}

byte Readbyte(int DeviceAddress, byte DataAddress)
{
  byte rdata = 0xFF;
  Wire.beginTransmission(DeviceAddress);
  Wire.write(DataAddress);
  Wire.endTransmission();
  Wire.requestFrom(DeviceAddress, 1);
  delay(10);
  if (Wire.available())
  {
    rdata = Wire.read();
  }
  delay(10);
  return rdata;
}

void setup()
{
  Wire.begin();
  Serial.begin(115200);
  
  for (i=0;i<256;i++) {
    if (i< idLen) {
       Writebyte(0x50,i,id[i]);
    }
    else {
      // comment out the following line if values in the address greater than idLen not to be overrided with 0
      Writebyte(0x50,i,0);
    }
  }
  
  // Address 8 contains address storing switch status value '0' or '1'
  Writebyte(0x50,8,255);
  
  for (i=0;i<256;i++) {
    if ((i%colPerLine) == 0) Serial.println("");
    val = Readbyte(0x50,i);
    Serial.print(val);
    Serial.print(" ");
  }
}

void loop()
{
    // if (i > 0xFF)
    // {
    //   i = 0x00;
    // }
    // Writebyte(0x50, i, i);
    // val = Readbyte(0x50, i);
    // i++;
    // Serial.print(val);
    // Serial.print(" ");
    // if ((i%8) == 0) Serial.println("");
}

2014/11/09

Arduino RF Wireless Message Display Using I2C LCD Keypad Shield

The function of this sketch is same as the sketch posted on May. 11, 2014, but using I2C LCD Keypad Shield as the output device.

Sketch

/*
SimpleReceiveI2CLCD
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 11
Modified by Befun Hung on Nov. 09, 2014 
Use I2C LCD Keypad Shield as the output
*/

#include <VirtualWire.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// #define BACKLIGHT_PIN 13
LiquidCrystal_I2C lcd(0x20); //Set the LCD I2C address
// LiquidCrystal_I2C lcd(0x38, BACKLIGHT_PIN, POSITIVE);

byte message[VW_MAX_MESSAGE_LEN]; // a buffer to store the incoming messages
byte messageLength = VW_MAX_MESSAGE_LEN; // the size of the message
unsigned long counter = 1;

void setup()
{
  // pinMode(BACKLIGHT_PIN, OUTPUT);
  // digitalWrite(BACKLIGHT_PIN, HIGH);
  // set up the LCD's number of columns and rows: 
  lcd.begin(16, 2); //initialize the lcd
  // Print a message to the LCD.
  lcd.home(); //go home
  lcd.print("Device is ready!");
  // 
  Serial.begin(9600);
  Serial.println("Device is ready");
  // Initialize the IO and ISR
  vw_setup(2000); // Bits per sec
  vw_rx_start(); // Start the receiver
}
void loop()
{
  if (vw_get_message(message, &messageLength)) // Non-blocking
  {
    lcd.clear();
    Serial.print(counter);
    Serial.print(": ");
    // set the cursor to column 0, line 1
    // (note: line 1 is the second row, since counting begins with 0):
    lcd.setCursor(0, 0);
    lcd.print(counter);
    lcd.print(":");
    lcd.setCursor(0,1);
    for (int i = 0; i < messageLength; i++)
    {
      Serial.write(message[i]);
      lcd.print(char(message[i]));
    }
    Serial.println();
    counter++;
  }
}

2014/08/09

Schematic and Photo Of Arduino PCF8574 I2C LCD Keypad Shield

  My PCF8574 I2C LCD Keypad Shield has been completed. The benefit of I2C LCD Keypad Shield over LCD Keypad Shield is that it free up 6 digital I/O used by LCD Keypad Shield for other use.
  The schematic used is version 1.1 other than the version 1.0 posted on Feb. 28, 2014. The difference between version 1.1 and version 1.0 is that the version 1.1 has a 4-pins I2C connector as shown on the bottom-left corner of the shield on the photo to act a either a shield or a breakout. When the shield serves as a breakout, the 5-way button cannot function. The sketch runs is same as the one posted on Feb. 28, 2014.

  When the shield is stacked directly upon the Arduino Uno, it should be noticed avoiding the pins of PCF8574 touching the shield of USB type B connector of Arduino. A insulated stick is a simple way. It is recommend to use Arduino boards with mini or micro USB connector such as Leonardo or stack on any other shield.


Schematic
Photo

2014/02/28

Schematic For PCF8574 I2C LCD Keypad Shield Using LiquidCrystal_I2C.h Library

By using PCF8574 I2C port extender, the LCD Keypad Shield can be released 6 digital I/O ports used to control HD44780 compatible LCD by using only SDA and SCL (Arduino A4 and A5). Part of the following schematic is implemented on breadboard to show result on LCD module, run the example sketch of LiquidCrystal_I2C.h library by changing the address to 0x20, the result is showed on the following photo.


Schematic

Photo

Sketch

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

#define BACKLIGHT_PIN     13

LiquidCrystal_I2C lcd(0x20);  // Set the LCD I2C address

//LiquidCrystal_I2C lcd(0x38, BACKLIGHT_PIN, POSITIVE);  // Set the LCD I2C address

// Creat a set of new characters
const uint8_t charBitmap[][8] = {
   { 0xc, 0x12, 0x12, 0xc, 0, 0, 0, 0 },
   { 0x6, 0x9, 0x9, 0x6, 0, 0, 0, 0 },
   { 0x0, 0x6, 0x9, 0x9, 0x6, 0, 0, 0x0 },
   { 0x0, 0xc, 0x12, 0x12, 0xc, 0, 0, 0x0 },
   { 0x0, 0x0, 0xc, 0x12, 0x12, 0xc, 0, 0x0 },
   { 0x0, 0x0, 0x6, 0x9, 0x9, 0x6, 0, 0x0 },
   { 0x0, 0x0, 0x0, 0x6, 0x9, 0x9, 0x6, 0x0 },
   { 0x0, 0x0, 0x0, 0xc, 0x12, 0x12, 0xc, 0x0 }  
};

void setup()
{
   int charBitmapSize = (sizeof(charBitmap ) / sizeof (charBitmap[0]));

  // Switch on the backlight
  pinMode ( BACKLIGHT_PIN, OUTPUT );
  digitalWrite ( BACKLIGHT_PIN, HIGH );
  
  lcd.begin(16,2);               // initialize the lcd 

   for ( int i = 0; i < charBitmapSize; i++ )
   {
      lcd.createChar ( i, (uint8_t *)charBitmap[i] );
   }

  lcd.home ();                   // go home
  lcd.print("Hello, ARDUINO ");  
  lcd.setCursor ( 0, 1 );        // go to the next line
  lcd.print (" FORUM - fm   ");
  delay ( 1000 );
}

void loop()
{
   lcd.home ();
   // Do a little animation by writing to the same location
   for ( int i = 0; i < 2; i++ )
   {
      for ( int j = 0; j < 16; j++ )
      {
         lcd.print (char(random(7)));
      }
      lcd.setCursor ( 0, 1 );
   }
   delay (200);
}


2013/12/29

Notes About PCF8574 I2C LCD Using LiquidCrystal_I2C.h


IC Manufacturer
NXP - PCF8574P, PCF8574AP
TI - PCF8574N

Library
LiquidCrystal_I2C.h by Francisco Malpartida download from BitBucket Repository

Address
I2C address of PC8574 (all three address inputs grounded) = 0x20
I2C address of PC8574A (all three address inputs grounded) = 0x38

Pins Mapping (16 Pins 8574) (Schematic)
PCF8574.Pin1 -> GND
PCF8574.Pin2 -> GND
PCF8574.Pin3 -> GND
PCF8574.Pin4 (Port0) -> LCD.Pin11 (DB4)
PCF8574.Pin5 (Port1) -> LCD.Pin12 (DB5)
PCF8574.Pin6 (Port2) -> LCD.Pin13 (DB6)
PCF8574.Pin7 (Port3) -> LCD.Pin14 (DB7)
PCF8574.Pin9 (Port4) -> LCD.Pin4 (RS)
PCF8574.Pin10 (Port5) -> LCD.Pin5 (WR)
PCF8574.Pin11 (Port6) -> LCD.Pin6 (EN)
PCF8574.Pin14 (SCL) -> UNO.A5
PCF8574.Pin15 (SDA) -> UNO.A4
PCF8574.Pin16 (VDD) -> UNO.5V
PCF8574.Pin8 (VSS) -> UNO.GND

LCD Initialization
#include <Wire.h> // comes with Arduino IDE
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x20)
lcd.begin(16,2) // in Setup()

Variations (Yourduino)
   Marked "YwRobot Arduino LCM1602 IIC V1" -> lcd.begin(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE) // also SainSmart LCD2004, DFRobot (DZRMO)
   Marked "Arduino-IIC-LCD GY-LCD-V1" -> lcd.begin(0x20, 4, 5, 6, 0, 1, 2, 3, 7, NEGATIVE)
   Marked "LCD1602 IIC A0 A1 A2" -> lcd.begin(0x20, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE)

Example Code (Yourduino)
/* YourDuino.com Example Software Sketch
 16 character 2 line I2C Display
 Backpack Interface labelled "YwRobot Arduino LCM1602 IIC V1"
 terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <Wire.h>  // Comes with Arduino IDE
// Get the LCD I2C Library here: 
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
// Move any other LCD libraries to another folder or delete them
// See Library "Docs" folder for possible commands etc.
#include <LiquidCrystal_I2C.h>

/*-----( Declare Constants )-----*/
/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x20);  // Set the LCD I2C address

/*-----( Declare Variables )-----*/
//NONE

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
  Serial.begin(9600);  // Used to type in characters

  lcd.begin(16,2);   // initialize the lcd for 16 chars 2 lines, turn on backlight

// ------- Quick 3 blinks of backlight  -------------
  for(int i = 0; i< 3; i++)
  {
    lcd.backlight();
    delay(250);
    lcd.noBacklight();
    delay(250);
  }
  lcd.backlight(); // finish with backlight on  

//-------- Write characters on the display ------------------
// NOTE: Cursor Position: (CHAR, LINE) start at 0  
  lcd.setCursor(0,0); //Start at character 4 on line 0
  lcd.print("Hello, world!");
  delay(1000);
  lcd.setCursor(0,1);
  lcd.print("HI!YourDuino.com");
  delay(8000);  

// Wait and then tell user they can start the Serial Monitor and type in characters to
// Display. (Set Serial Monitor option to "No Line Ending")
  lcd.clear();
  lcd.setCursor(0,0); //Start at character 0 on line 0
  lcd.print("Use Serial Mon");
  lcd.setCursor(0,1);
  lcd.print("Type to display");  


}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
  {
    // when characters arrive over the serial port...
    if (Serial.available()) {
      // wait a bit for the entire message to arrive
      delay(100);
      // clear the screen
      lcd.clear();
      // read all the available characters
      while (Serial.available() > 0) {
        // display each character to the LCD
        lcd.write(Serial.read());
      }
    }
  }

}/* --(end main loop )-- */


/* ( THE END ) */