Connecting a Raspberry Pi to an Arduino Uno Using the I2C Protocol Part 2: Analogue Voltages.

Did you know that the Raspberry Pi was never intended for use in hobby electronics and prototyping?

According to a recent interview with creator Eben Upton, the decision to connect the Broadcom chip’s GPIO pins to a 40 pin header was really an afterthought. At the time, he wasn’t much convinced that anyone would use it.


tArduino boards, on the other hand, are built for little else but electronic prototypes, which is why they come with quite a bag of tricks. Key among these are its usefulness in working with analogue voltages: those voltage levels that aren’t all the way on, or all the way off, but somewhere in the middle.


Connecting an Arduino Uno to your Raspberry Pi’s I2C bus is a great way to add these analogue voltage capabilities to your Raspberry Pi project.

This follows on from part 1 of our guide to using I2C to connect a Raspberry Pi to an Arduino Uno.

Simulating Analogue Voltages With PWM
PWM – or pulse width modulation – is a way of varying the amount of power a pin provides by switching it off and on really fast.

This isn’t a true analogue voltage, but for something like dimming an LED, you’d never notice the difference.

For tasks that require more power than the PWN pin can provide directly, you can use it with a transistor. With this arrangement, you can vary the speed of a DC motor.

Do You Actually Need an Arduino For This?


The Raspberry Pi has two hardware PWM pins and if that’s all you need, great! Be aware that the Raspberry Pi uses the same PWM hardware for the built-in analogue headphone output, so it’s not a great idea to use them simultaneously.

You can also use software to generate a PWM signal from any GPIO pin. The disadvantage of this is that it puts you at the mercy of the operating system, which may introduce tiny interruptions as it juggles tasks. That’s fine when there are no grave consequences for the occasional small inaccuracy. For a robot or industrial machinery, it’s not so great.

The Arduino Uno has six hardware PWM pins. If you still need more, the Arduino Mega offers 15.

And because this is I2C, if one microcontroller isn’t enough, you can connect a hundred more to the same bus.

Using I2C to Control the Arduino Uno PWM:

Ok, let’s put this into practice.

The Arduino IDE makes PWM easy using the analogWrite() function. This takes two arguments: the first is the pin number, and the second is a value between 0 (for always off) and 255 (for always on).

You don’t need to import anything to use analogWrite(). Be sure that you are using it with a pin that connects to PWM hardware. On the Arduino Uno, those pins are 3, 5, 6, 9, 10 and 11.

We can read the I2C bus for analogWrite() values like this:

#include

const int led = 9;
const int addr = 0x8;

void setup() {
// Joins the I2C bus as a slave at address 0x8
Wire.begin(addr);
// Sets pin 9 as an output pin
pinMode(led, OUTPUT);

// Calls the changeBrightness function when the master sends a value
Wire.onReceive(changeBrightness);
}

// Reads and sets the PWM value from the I2C bus
void changeBrightness(int bitstream) {
int brightness = Wire.read();
analogWrite(led, brightness);
}

void loop() {
}

Upload this sketch to your Arduino Uno and then connect it to the Raspberry Pi’s I2C, ground and power pins, just as we did in part 1.

Now we can use the smbus module to connect to the I2C bus. Again, it’s the same as what we learned in part 1.

from smbus import SMBus
arduino = 0x8
i2cbus = SMBus(1)

Just like before, we can use the write_byte method to write values to the I2C bus.
i2cbus.write_byte(arduino, 180)
i2cbus.write_byte(arduino, 35)
i2cbus.write_byte(arduino, 250)
i2cbus.write_byte(arduino, 120)

Does this alter the brightness of the LED? If so, great!

Comments

Popular posts from this blog

Smartphone Controlled Arduino Rover.

How to Build a Robot Line Follower without a Controller