|

Nei nostri progetti con Arduino, il risparmio dei pin è una cosa fondamentale. Come fare allora a collegare un tastierino tipo quello in foto utilizzando il minor numero di pin possibile? Semplicemente utilizzando un GPIO port expander i2c a 8bit. Se non conoscete il protocollo I2C potete leggervi questo articolo: Il protocollo I2C; oppure se non conoscete i port expander potrete leggere quest'altro articolo: GPIO Port Expander.
Per espandere gli ingressi di arduino utilizzeremo appunto il port expandere PCF8574P e lo piloteremo utilizzando questa libreria: i2ckeypad.
Al solito scaricate la libreria, scompattatela e mettetela nella cartella libraries dentro la cartella dell ide.
Lo schema da seguire e` questo:
NB: Non ci sono le resistenze di pull up quindi aggiungetene una su sda e una su scl da 4k7. L'indirizzo del seguente port expander sarà 0x20, ma voi potrete dare l'indirizzo che vorrete collegando A0 A1 e A2 a massa o a VCC. Gli indirizzi possibili sono da 0x20 a 0x27. R1, R2 e R3 sono da 10K.
Lo sketch da provare per vedere se tutto funziona è questo:
Includete le librerie: i2ckeypad.h e wire.h
/*
* i2ckeypad.pde - keypad/I2C expander interface example for Arduino
*
* Copyright (c) 2009 Angel Sancho
* All rights reserved.
*
*
* LICENSE
* -------
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#define ROWS 4
#define COLS 3
//Con questa riga definite l'indirizzo del PCF8574P.
#define PCF8574_ADDR 0x20
i2ckeypad kpd = i2ckeypad(PCF8574_ADDR, ROWS, COLS);
void setup()
{
Serial.begin(9600);
Wire.begin();
kpd.init();
Serial.print("Testing keypad/PCF8574 I2C port expander arduino lib\n\n");
}
void loop()
{
char key = kpd.get_key();
if(key != '\0') {
Serial.print(key);
}
}
Articolo completamente scritto da Calamaro, l'immagine è tratta da Sparkfun.com
![]()
