You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 line
2.2KB

  1. /*
  2. Digital Pot Control
  3. This example controls an Analog Devices AD5206 digital potentiometer.
  4. The AD5206 has 6 potentiometer channels. Each channel's pins are labeled
  5. A - connect this to voltage
  6. W - this is the pot's wiper, which changes when you set it
  7. B - connect this to ground.
  8. The AD5206 is SPI-compatible,and to command it, you send two bytes,
  9. one with the channel number (0 - 5) and one with the resistance value for the
  10. channel (0 - 255).
  11. The circuit:
  12. * All A pins of AD5206 connected to +5V
  13. * All B pins of AD5206 connected to ground
  14. * An LED and a 220-ohm resisor in series connected from each W pin to ground
  15. * CS - to digital pin 10 (SS pin)
  16. * SDI - to digital pin 11 (MOSI pin)
  17. * CLK - to digital pin 13 (SCK pin)
  18. created 10 Aug 2010
  19. by Tom Igoe
  20. Thanks to Heather Dewey-Hagborg for the original tutorial, 2005
  21. */
  22. // include the SPI library:
  23. #include <SPI.h>
  24. // set pin 10 as the slave select for the digital pot:
  25. const int slaveSelectPin = 10;
  26. void setup() {
  27. // set the slaveSelectPin as an output:
  28. pinMode (slaveSelectPin, OUTPUT);
  29. digitalWrite (slaveSelectPin, HIGH);
  30. // initialize SPI:
  31. SPI.begin();
  32. }
  33. void loop() {
  34. // go through the six channels of the digital pot:
  35. for (int channel = 0; channel < 6; channel++) {
  36. // change the resistance on this channel from min to max:
  37. for (int level = 0; level < 255; level++) {
  38. digitalPotWrite(channel, level);
  39. delay(10);
  40. }
  41. // wait a second at the top:
  42. delay(100);
  43. // change the resistance on this channel from max to min:
  44. for (int level = 0; level < 255; level++) {
  45. digitalPotWrite(channel, 255 - level);
  46. delay(10);
  47. }
  48. }
  49. }
  50. void digitalPotWrite(int address, int value) {
  51. // gain control of the SPI port
  52. // and configure settings
  53. SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
  54. // take the SS pin low to select the chip:
  55. digitalWrite(slaveSelectPin,LOW);
  56. // send in the address and value via SPI:
  57. SPI.transfer(address);
  58. SPI.transfer(value);
  59. // take the SS pin high to de-select the chip:
  60. digitalWrite(slaveSelectPin,HIGH);
  61. // release control of the SPI port
  62. SPI.endTransaction();
  63. }