//
you're reading...
Projects

Using the PCF8574 IO expander with the EV3

2014-09-26 11.25.32

Mindsensors sell a breadboard sensor making kit with a PCF8574 for the NXT. I bought one a long time ago, and I thought I would try it on the EV3, after seeing some interest in the chip on the leJOS forums.

I am using the sensor in read mode to read a set of buttons. You can also use it in write mode to switch up to 8 devices. For example, you could use it to drive a seven-segment LED, or to control motors.

I had to modify the ev3classes project to allow read only i2c transactions. All the other sensors we currently support use a register value that is written to the device. The PCF8754 just reads or writes a single byte of data. This means the code below will only work on the Git master version of leJOS or the 0.9.0 release when it comes out.

The code for my read mode sample provider is:

import lejos.hardware.port.Port;
import lejos.hardware.sensor.I2CSensor;
import lejos.hardware.sensor.SensorMode;
import lejos.robotics.SampleProvider;

public class PCF8574Sensor extends I2CSensor  {

	public PCF8574Sensor(Port port) {
		super(port, 0x40);
		setModes(new SensorMode[] { new ReadMode()});
	}
	
	private class ReadMode implements SensorMode {
	
	    @Override
	    public int sampleSize() {
	    	return 1;
	    }
	
	    @Override
	    public void fetchSample(float[] sample, int offset) {
	    	byte[] buf = new byte[1];
	    	port.i2cTransaction(0x40, null, 0, 0, buf, 0, 1);
			sample[offset] = buf[0] & 0xFF;
	    }
	
	    @Override
	    public String getName() {
	    	return "Read";
	    }
	}
	
	public SampleProvider getReadMode() {
		return getMode(0);
	}
}

And my test program:

import lejos.hardware.Button;
import lejos.hardware.lcd.LCD;
import lejos.hardware.port.SensorPort;
import lejos.robotics.SampleProvider;
import lejos.utility.Delay;

public class PCF8574Test {
	public static void main(String[] args) {
		PCF8574Sensor sensor = new PCF8574Sensor(SensorPort.S1);
		SampleProvider sp = sensor.getReadMode();
		float[] sample = new float[sp.sampleSize()];
		LCD.drawString("Binary:", 0, 2);
		LCD.drawString("Decimal:", 0, 3);
		
		while(Button.ESCAPE.isUp()) {
			sp.fetchSample(sample,0);
			LCD.drawString(String.format("%8s", Integer.toBinaryString((int) sample[0] & 0xff)).replace(' ', '0'), 9, 2);
			LCD.drawInt((int) sample[0] & 0xff, 3, 9, 3);
			Delay.msDelay(1000);
		}	
		sensor.close();
	}
}
Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

About leJOS News

leJOS News keeps you up-to-date with leJOS. It features latest news, explains cool features, shows advanced techniques and highlights amazing projects. Be sure to subscribe to leJOS News and never miss an article again. Best of all, subscription is free!
Follow leJOS News on WordPress.com
%d bloggers like this: