Week 11:

Input Devices

Researching about The Sensors I decided to work with

For my Final project I will need positional feedback from a rotating arm. For which I plan to use a quadrature Encoder. For our Machine Design project, we used the ADXL345 accelerometer and a joystick module. The Detailed Interfacing documentation for these two sensors I decided to add on this week.

The four sensors I will be covering here are;

  • ADXL345 Accelerometer
  • Joystick Module
  • Rotary Encoder KY-040
  • Quadrature Optical Encoder

ADXL345 Accelerometer

Principle of Operation

Before we get to specific description of our device, let's take generalized look at how MEMs Accelerometers work. The following video from Bosch explains it perfectly.



Basically these tiny chips have a vibrating comblike mechanism inside it which changes capacitance depending on it's position. There is a Motion Processing Unit along this microstructure which adds the intelligence to the device. Other than the variable capacitive accelerometers, there are also Piezoresistive Accelerometers. The Basic idea about them can be gathered from this very informative link on Acceletometers by PCB.com.

Specification:

The ADXL345 is a small, thin, ultralow power, 3-axis accelerometer with high resolution (13-bit) measurement at up to ±16 g. Digital output data is formatted as 16-bit twos complement and is accessible through either a SPI (3- or 4-wire) or I2C digital interface.

  • Power consumption scales automatically with bandwidth
  • User-selectable resolution
  • Patent pending, embedded memory management system with FIFO technology minimises host processor load
  • Single tap/double tap detection
  • Activity/inactivity monitoring
  • Free-fall detection
  • SPI (3- and 4-wire) and I2C digital interfaces
  • Flexible interrupt modes map able to either interrupt pin
  • Measurement ranges selectable via serial command
ADXL345 (LGA-14 package)
Although this device was supposed to be a part of the fab inventory, out lab didn't have it. But I was so impressed with it's features (such as the freefall & tap detection), I decided to source one myself from here.

This video by Analog Devices explains the smart features of the ADXL345.



Joystick Module

Working Principle

There's nothing really new about joystick modules. Since the very early days of the atari joysticks, to modern day joystick modules, they all have the same working principal. Each axis is represented by a variable resistance (just a potentiometer). This video by the EEVblog explains the inner workings of a joystick and how the interfacing used to work before ADCs got so cheap.



Specification

  • Dimensions: 40 x 27 x 15 (LxWxH) mm.
  • Weight: 10gm (without Hat).
  • 2.54mm pin interface leads.
  • Operating Voltage: 5V.
  • Long service life and stable performance.
  • Standard interface and electronic building blocks.
  • Widely use in Arduino DIY projects.
Generic PS2 Joystick Module

KY-040 Rotary Encoder Module

Principle of Operation:

To Understand the principle of operation of a rotary encoder, this video from sparkfun was very helpful.

The Encoder I am using is a mechanical rotary encoder. This illustration from the datasheet explains how the 2-bit gray code is generated. As you can see, the angular position of the A terminal and the B terminal is such that:

  • Rotating the switch clockwise will cause the switch connecting A and C to change states first.
  • Rotating the switch counterclockwise will cause the switch connecting B and C to change states first.

If we were to represent the opening and closing of the switches as wave forms, it would look something like this;

Essentially, determining which switch changed states first is how the direction of rotation is determined. If A changed states first, the switch is rotating in a clockwise direction. If B changed states first, the switch is rotating in a counter clockwise direction.

Specification

The rotary Encoder I sourced from a local shop here was KY-040. The datasheet for it can be found here. The specifications are:

  • Operating voltage: 5V.
  • Pulses/360° Rotation: 20.
  • Output: 2-bit gray code
  • Mechanical Angle: 360° continuous.
  • With built in push button switch (push to operate)
  • Dimensions: (30 x 18 x 30) mm.
  • Compatible with Arduino/Raspberry Pi controller board.

Quadrature Optical Encoders

Principle of Operation

The Principle of operation for the optical encoders are the same for our rotary encoder earlier. Instead of mechanical contacts, this encoder uses IR photo transistors instead which gives much more precise data also much faster. Since it does not have any mechanical contact, it works really good at high RPMs.

Specification:

  • Number of lines: 100 lines.
  • Outer diameter: 22mm.
  • Hole diameter: 3.5mm.
  • Thickness: 0.3mm.
  • Material: alloy steel.
  • Production process: laser cutting.
  • Supply voltage: 5V (3.3V is also available).
  • Wiring: Red 5V / Black GND.
  • Output signal: 5Vp-p two-phase signal output.
  • Two signal lines, one yellow, one blue.
  • Yellow and blue are two TTL level signals (can be directly connected microcontroller)
  • (Note: Measuring Reversible rely on these two levels to distinguish between forward or reverse).

Making a 328p board for the Accelerometer & Joystick Module

The ADXL345 is a 3.3v chip and we don't have any level shifter ICs at the lab, so I decided to work with 3.3v for this board. While designing the board, I had these following constraints in mind,

  • i2c interface with ADXL345 and all necessary filtering capacitors should be there.
  • Breakout for an analog joystick module should be present.
  • Interface for communication module (nrF240l1+ and Bluetooth HC-05) should be present
  • The board should be running on 3.3v on external battery power, which means it would need a decent enough power regulation circtuit.
  • I wanted to keep plug and play style connectors for everything, which would add alot of zero ohm resistors to the board, but none of the connectors needed extra jumper wires.
More on this board can be found on the Networking & communication Week and Machine Design Week. I decided to add this documentation here because it is more relevant with Inputs.

With some research I found out the Atmega328p can run only on 8MHz on 3.3v. So I used an 8MHz resonator instead of the usual 16MHz or 20MHz I usually use.

I kept a seperate AMS1117 to power the nRF240L01 module as I read that they get a bit fussy if proper power is not supplied.

I used Sparkfun's ADXL345 breakout board as a reference for interfacing the ADXL345 chip. I tied pin 7 with VCC which enabled i2c mode and added noise filtering capacitors of 0.1uF and 10uF near the chip.

The board can be run on battery power as the voltage level needs to be just above 3.3 to work.

Schematic
Board Layout

Milling & Soldering:

Calculating the 1/64 traces toolpath on mods
After 1/32 outline toolpath on mods
After 1/64 traces were milled
Board Cutout, did some minor sanding with 1000 grit sandpaper
Soldered all 0 ohm resistors first
Soldered 328p and resonator
Was the Hardest to solder, LGA-14 and I only had one piece of this

Finished Boards:

Finished Board
Ran out of power jacks so swapped it out with a 0.65mm jack. All versions present in the Eagle project.
Download Eagle Project Files

Interfacing the ADXL345 Accelerometer with a 328p board

->Checking if Accelerometer is Detected:

Since this was the first time I soldered a component without any legs, I wanted to check if my accelerometer was being detected as an i2c device, so I used the i2c Scanner code from Arduino Playground and it seemed to be getting detected at addres 0x53.




Reading Accelerometer Data:

To read the accelerometer data, I used the Sparkfun ADXL345 Library. It had many smart features like tap, double tap, activity, inactivity and freefall detection. But for now, I'll just be using the following methods to get the x,y,z data.

Important LibraryFunctions Used


Interfacing Analog Joystick Module with a 328p board

The Joystick Module was much easier to interface. In my schematic, x and y were connected to ADC6 and ADC7 respectively. The corresponding Arduino pins for the are A6 and A7. The connection Diagram so far was;

  • Joystick ------ Controller Board
  • X--------------A6
  • Y--------------A7
  • VCC(3.3v)------VCC
  • GND------------GND
  • SW-------------N/C
  • ( I left the pushbutton not connected as I had no use for it)

Then I wrote a simple code to read analog data from pin A6 and A7 and print it on Serial.

Designing & Making the PxFDuino v1.0

The PxFDuino is the board I made with my Final Project in Mind. All the relevant documentation on it can be found here in my Final Project Documentation. I'll be using this board to interface the Encoders with.

Interfacing Mechanical Rotary Encoder KY-040 with PxFDuino

Using digitalRead:

First I tried taking count of the encoder data just by taking digital read. I incremented the counter when channel 1 was leading and decremented when chanel 2 was leading. The logic of the code was as simple as that. Reference to where I got the idea from can be found here.

Using Hardware Interrupts

Rotary Encoders are perfect use cases for external Interrupt pins. They wont clog up your main loop and only execute when the interrupt service routine is executed.(In this case, a rising or falling edge on the encoder pins) This is a main reason why I couldn't use a ATTINY44 based board as the Arduino Serial library for that is based on the ISR. So if you want to use Serial communication, you wont be able to use pin change interrupts. A solution though is to use the USI serial or the hardware serial on the ATTINY44. But I didn't want to make another ATTINY44 board and just switched to using a 328p which I am comfortable with. Now for the ISR based rotary encoder code, I found a very optimized code in this Instructable. and will be reusing it.

Video:



Interfacing Generic Chinese Optical Encoders with the PxFDuino

First attempt to interface this sensor literally went up in smoke, the photodiode had burnt out. I could not figure out what happened and made a post on Stack Exchange where I got some positive enough responses to order a few more of these.

The new encoders that I recieved did not burn out as soon as I connected them, but they were not working either. I was only getting gibberish data. I read about encoder wheel alignment from this article by amscontrols and ended up 3d printing a mount for my enocder to match with the wheel. All the files regarding this can be found here on my final project page.

After a whole two or three weeks of trying to interface these encoders and failing I posted an issue on the class issue tracker to find an alternative. The reason for it not working, I tracked down to three probable causes;

  • The Encoder wheel position needs to be perfectly aligned
  • The generic chinese photodiode was not giving any reading on channel 2, even on the oscilloscope. Perhaps they can only count in one direction?
  • These sensors are not TTL compatible, they will need additional voltage divider circuitry and filtering capacitors to work
Lesson learnt, NO DATASHEET = NO BUY! Don't trust your electronics skills to get any random generic sensor working on a tight schedule.

The good thing that came out of the issue tracker, was that Neil suggested me to make my own optical encoders. I spent a few days trying to source the parts, which were not available at my lab and ended up making a magnetic encoder instead. The documentation for which can be found here.

Probing Sensor Data with Oscilloscope [Groupwork]

For our groupwork this week, we decided to probe the Quadrature Encoders.

Probing Quadrature Encoders

Quadrature encoders are perfect sensors to probe with an oscilloscope.

  • They require 2 channels, which will give us a nice demo to watch.
  • They are timer dependent, channel 1 leading will mean it's going in one direction while channel 2 leading will mean it's going on the other direction.

Connection:

  • Board ----------------Oscilloscope
  • GND ----------------- GND probe for both channel 1 and channel 2
  • CLK ----------------- Channel 1 positive probe
  • DT ----------------- Channel 2 positive probe

Then we pressed 'autoset' on the scope to give us the following video



Learning Outcomes

  • I learnt how to probe sensor data and debug them
  • I learnt a hard lesson not to trust generic sensors with not datasheets, always go for the better known brands or stuff which have adequate documentation on them.
  • I learnt about interrupt handlers and how to utilize them to take readings outside the main loop in programming.