Category Archives: Electronics

Rotary Encoders in Colour on the i2c Bus

This is a follow-up to my previous writings on the subject of rotary encoders: Rotary Encoders on the i2c Bus. This time I am using the Sparkfun Rotary Encoder – Illuminated (RGB) (Part: COM-10982), this has the same rotary goodness as the SparkFun 12-step rotary encoder but with the addition of three LED’s to provide a whole host of colours on the rotating shaft.

Test Setup

Again I will be using the MCP23017 port expander to add 16 digital I/O ports to the Arduino via the i2c bus,  the rotary encoder part operates in the same manner as before and we can use the internal pull-up resistors to reduce the number of components. The LED’s operate with a common anode and the push button also operates on 5v rather than the usual switching to ground.

For my test setup I have connected the rotary encoder to GPA0 and GPA1, the push button to GPA2 and the Red, Green and Blue LED’s to GPA5, GPA4 and GPA3. Note the 10K pull-down resistor on the push button.

My program on the Arduino changes the colours as you rotate the shaft, you will see seven colours, to see more you would need to use PWM to control the LED’s brightness. With the common anode on the LED’s the logic for switching them is inverted, so HIGH = off, LOW = on. You will need the Adafruit MCP23017 Arduino Library.

Links

Rotary Encoders on the i2c bus

I have been getting to grips with rotary encoders on the Arduino, and to add a little drama I have gotten this working on the i2c bus. Here I will be showing how to set up the necessary hardware and demonstrate a program for the Arduino. I have used a similar setup to control an LED RGB light strip, with three rotary encoders to control the Red, Green and Blue and a fourth for special effects.

The i2c bus

The i2c bus allows connection of multiple devices to the Arduino on just two wires, these can be just about anything from temperature sensors to motor controllers with each device having its own address, up to eight of these can be used using just two wires from the Arduino.

For this project I’ll be using a single MCP23017 port expander, with which I can add sixteen digital I/O pins to the Arduino

MCP23017 pinout

The address for the expander is set on pins 15, 16 and 17 (A0, A1 and A2), for a single encoder set all of these to ground. Should you require more expanders the addresses can be set as in the table below, the MCP address is for the Arduino program.

chip
address
hardwired address i2c
address
MCP
address
A2 A1 A0
000 GND GND GND 0x20 0
001 GND GND 3v3 0x21 1
010 GND 3v3 GND 0x22 2
011 GND 3v3 3v3 0x23 3
100 3v3 GND GND 0x24 4
101 3v3 GND 3v3 0x25 5
110 3v3 3v3 GND 0x26 6
111 3v3 3v3 3v3 0x27 7

Rotary Encoders

The  encoders I’m using are the SparkFun 12-step rotary encoder with integrated push-button

rotary encoder

Inside they have mechanical contacts that output two square waves when rotated, A and B these are 90o out of phase with each other so when rotated clockwise output A is ahead of B, and counter-clockwise output B takes the lead, this is a two bit Grey code.

rotary encoder square wave
rotary switch outputs (source: datasheet)

So by comparing the two outputs we can determine the direction of rotation.

A Test Circuit

My test setup comprises of two rotary encoders, one Arduino Uno, one MCP23017 port expander, and a couple of resistors. External pull-up resistors are not required on the GPx input ports as the MCP23017 has these internally. Encoder A uses GPA0, GPA1 and GPA2 for the push button. Encoder B is on GPA4, GPA5, and GPA6.

MCP23017 rotary encoder test circuit

Programming

My program uses the Adafruit MCP23017 and standard wire libraries. The Adafriut library addresses the GPx ports from 0-15, so GPA2 is 2, and GPB2 is 9. I have written this to output the state of rotation to the serial port at 9600 baud.

 

links:

Infra-Red Coin Detector for Arduino

For something I’m building I need have the Arduino detect a coin being dropped trough a slot, for this I have built an IR detector, it comprises of an IR LED, IR Photo-Diode, Op-amp and ATtiny85 micro-controller.

IR Coin Detector (Mk3)

The circuit works by having the IR LED flood the Photo-diode so that when an object passes between them the Photo-diode stops letting current through, this is fed into the op-amp to provide a consistent output for the ATtiny85 micro-controller to detect the change in signal to then flash a couple of LED’s and provide a signal to an Arduino.

IR Coin Detector (Mk3)

Here is a program that flashes a couple of LED’s and makes output pin 4 high:

Sources:

  1. DIY Science: Measuring Light with a Photodiode II
  2. Pin Change Interrupt on the ATtiny
  3. Programming an ATtiny85 with an Arduino