Squirreltech

March 4, 2012

Digi Pots 2!

Filed under: Electrical Engineering — Tags: , , — squirreltech @ 11:48 pm

I found this application called Fritzing that allow for easy schematic writing and image exporting…. So here is the schematic for the code in the last post.

 

Unfortunately, I realized I’ll need to go back to the drawing board… A speaker system has two “channels”, one for left and one for right. And so I’ll need each channel to go through two potentiometers….

March 1, 2012

Digi Pots!

Filed under: Electrical Engineering — squirreltech @ 1:15 am

So, I knew from school (high school I think) that a potentiometer allows for variable resistance. You see them when you turn a knob or a wheel to change the volume. My current project is to replace the volume knob on my nice speaker set with a device that will listen for the up/down volume control from my TV remote.

I broke this down into several phases:

  1. Read stuff from my tv remote
  2. Know which button was pressed on the remote and output something saying so
  3. Light a different LED based on the button pressed (i.e. red for up, yellow for down)
  4. Control a potentiometer via arduino
  5.  Put it all together

I manged to track down an IR Receiver and steps 1, 2, and 3 were relatively straight forward. The hard part came with the potentiometer. I managed to track down that what I needed was a “digital potentiometer”. Did some quick research, entered my credit card # and then I had three of them on their way. Sweet… er, now what? This thing has a lot more pins than an LED. 8 to be exact:

The MCP41X digital potentiometer

Well, many thanks to this post and specifically the notes about what pins they used:

  * All A pins  of AD5206 connected to +5V
  * All B pins of AD5206 connected to ground
  * An LED and a 220-ohm resisor in series connected from each W pin to ground
  * CS - to digital pin 10  (SS pin)
  * SDI - to digital pin 11 (MOSI pin)
  * CLK - to digital pin 13 (SCK pin)

So, I matched that description to the MCP41X data sheet and uploaded the code found in that example and lo and behold my LED got brighter and dimmer!!! Wooo!

The last step was to make it user controlled, so I added in some Serial.read stuff and took out the loops and here is the resulting code:

// From here:
// http://www.arduino.cc/en/Tutorial/SPIDigitalPot
/*
 Digital Pot Control

 This example controls an Analog Devices AD5206 digital potentiometer.
 The AD5206 has 6 potentiometer channels. Each channel's pins are labeled
 A - connect this to voltage
 W - this is the pot's wiper, which changes when you set it
 B - connect this to ground.

 The AD5206 is SPI-compatible,and to command it, you send two bytes,
 one with the channel number (0 - 5) and one with the resistance value for the
 channel (0 - 255). 

 The circuit:
 * P0A connected to +5V
 * P0B connected to ground
 * An LED and a 220-ohm resisor in series connected from P0W to ground
 * CS - to digital ardunio pin 10 (SS pin)
 * SDI - to digital ardunio pin 11 (MOSI pin)
 * CLK - to digital ardunio pin 13 (SCK pin)

 * Vdd to +5V?
 created 10 Aug 2010
 by Tom Igoe

 Thanks to Heather Dewey-Hagborg for the original tutorial, 2005

 */

// inslude the SPI library:
#include <SPI.h>

// set pin 10 as the slave select for the digital pot:
const int slaveSelectPin = 10;
const int numChannels = 1;
const int initialLevel = 64;
const int interval = 8;
const int maxLevel = 128;
int incomingByte = 0;
int level = initialLevel;
const int D_KEY = 100;
const int U_KEY = 117;
void setup() {
 // set the slaveSelectPin as an output:
 pinMode (slaveSelectPin, OUTPUT);
 // initialize SPI:
 SPI.begin();
 Serial.begin(9600);
}
void loop() {

 // send data only when you receive data:
 if (Serial.available() > 0) {
 // read the incoming byte:
 incomingByte = Serial.read();
// say what you got:
 Serial.print("I received: ");
 Serial.print(incomingByte, DEC);
 Serial.print( ": " );
if( incomingByte == U_KEY ) {
 Serial.println( "up!" );
 level += interval;
 digitalPotWrite(0, level);
 }
 if( incomingByte == D_KEY ) {
 Serial.println( "down!" );
 level -= interval;
digitalPotWrite(0, level);
 }
level = (level > maxLevel ) ? maxLevel : level;
 level = (level < 0 ) ? 1 : level;
 }
 Serial.print( "level: " );
 Serial.println( level );
}
// address is the "channel" of the "P". In this case there
// is only one channel so this will always be 0.
int digitalPotWrite(int address, int value) {
// take the SS pin low to select the chip:
 digitalWrite(slaveSelectPin,LOW);
 // send in the address and value via SPI:
 SPI.transfer(address);
 SPI.transfer(value);
 // take the SS pin high to de-select the chip:
 digitalWrite(slaveSelectPin,HIGH);
}

Create a free website or blog at WordPress.com.