Showing posts with label spi. Show all posts
Showing posts with label spi. Show all posts

Tuesday, May 30, 2017

Teensy 3.6 Basics: Using a Digital Pot

Overview
A digital pot is a digitally-controlled potentiometer, which is a very useful device. It allows a microcontroller, such as the Teensy, to change a potential or a resistance using data values instead of having to turn a physical pot with a human hand. There are many instances where a physical pot is used can be substituted with a digital pot.

Uses include: dimming LEDs, controlling the resistance of circuit bent toys, conditioning audio signals, setting the volume of audio signals, creating auto stereo panning devices, controlling an oscillator, creating a digital to analogue converter and so on.

The MCP4241 IC contains two digital pots, with each being 100k. The resolution is 7 bits for resistance. The two channels can be addressed independently. The Teensy 3.6 uses an SPI bus to communicate with the MCP4241.



Hardware Setup
The MCP4241 connects and communicates with a microcontroller (such as an Arduino or a Teensy) via the SPI Bus. SPI is a protocol that allows microcontrollers to interface easily with a large number of external chips and sensors.

SPI is a host / slave type bus. A single host microcontroller can interface with one or more slave chips or sensors.

SPI uses up to four pins for communication:
- CS (may also be called SS): chip select:
Used by the microcontroller to select each device. The host microcontroller has a CS output pin for every slave device that is to be used.

- SCK (or CLK): serial clock:
Used by the host microcontroller to time the data that is moved to and from the slave device. There is one SCK connection that is shared amongst all SPI devices.

- SDI (or MOSI): slave data in:
Used by the host microcontroller to send data to a slave device. There is one SDI connection that is shared amongst all SPI devices.

- SDO (or MISO): slave data out:
Used by the host microcontroller to receive data from a slave device. There is one SDO connection that is shared amongst all SPI devices.

All SPI slave devices require the CS and SCK pins. However, many device may not require both the SDI and SDO connections, as they might only either send or receive data.

With the MCP4241 chip, the resistance of a digital potentiometer is set using data from a host microntroller. As a result, only CS, SCK and SDI are required, because the information needs to travel only from the host to the slave device (i.e. from the Teensy to the MCP4241).

The hardware setup is shown below.

  • Digital output pin 10 of the Teensy is SPI Chip Select (CS or SS), and is connected to pin 1 of the MCP4241. 
  • Digital output pin 13 of the Teensy is SPI Clock (SCK or CLK), and is connected to pin 2 of the MCP4241. 
  • Digital output pin 11 of the Teensy is SPI digital output (MOSI or SDI). Teensy ground is connected to pin 4 of the MCP4241 (VSS). 
  • Teensy 3V is connected to pin 11, 12, 14 of the MCP4241 (WP or Write Protect, SHDN or Shutdown, and VDD respectively). 
The first digital pot is formed from pins 8, 9 and 10 while the second is pins 7, 6 and 5 of the MCP4241. 









Software Setup
The MCP4241 can be easily addressed using the SPI library in combination with writing the correct bytes and addresses to the chip. 

An example encapsulation is shown below: 


This function can then be used, as shown below in the example code. 



Example 1 - Incrementally Increase Resistance, and Loop
In this example, the digital pot is connected via the SPI bus. A multimeter is set to measure resistance with a range of 0 - 200K. The value of the digital pot is controlled so that the resistance increases by one step for every 100 milliseconds, looping after 128 steps. 






Summary
This is a simple example of how to use a digital pot with the Teensy 3.6. The digital pot can be used to map MIDI control to change in resistance, thus allowing a setup such as this to control, say, the volume and pitch of a 40106 oscillator (or a wide array of signals). 

Monday, November 21, 2016

Moog Mother-32: MIDI to Quad DAC CV Control




An example of using two MCP4922 that are controlled via MIDI. The four DAC channels are used to control filter frequency, filter resonance, LFO frequency and VCA modulation. 

In Ableton Live, four LFO generators are used to send MIDI continuous controller data out to the Teensy, which in turn then uses the data to set the DAC voltage level of the MCP4922 chips via the SPI bus.

Multiple MIDI-Controlled 12-Bit DACs via Teensy / Arduino

Overview
The MCP4922 is a 12-bit, two channel, SPI-based DAC - and can be easily connect to a studio setup using a Teensy via MIDI!This is an easy way of adding a 0 - 5V or 0 - 3.3V 4096-step DAC to a MIDI sequencing environment. Using one Teensy, multiple MCP4922 chips can be easily controlled, and mapped via MIDI data.

The cost of these chips is relatively low - around AUD$3 per chip or $1.50 per DAC channel - and they don't require any additional components for basic implementation. 



Hardware
Hardware connection is very similar to the previous post on the MCP4922. To connect multiple MCP4922 to a Teensy microcontroller, the following connections should be made:

  • Teensy 5V or 3.3V (depending on model) to MCP4922 VDD (Pin 1) - all MCP4922 have this connection in common
  • Teensy SPI Chip Select pin (user-designated) to MCP4922 CS (Pin 3) - each individual MCP4922 chip has a corresponding pin on the Teensy microcontroller. In this example, I am using digital pin 4 for the CS pin of MCP4922 #1 and then digital pin 5 for the CS pin of MCP4922 #2. 
  • Teensy SPI Serial Clock pin (as defined in the SPI library) to MCP4922 SCK (Pin 4) - all MCP4922 have this connection in common
  • Teensy SPI Digital Out (as defined in the SPI library) to MCP4922 SDI (Pin 5) - all MCP4922 have this connection in common
  • Teensy 5V or 3.3V (depending on model) to MCP4922 V REF A (Pin 13) - all MCP4922 have this connection in common
  • Teensy ground to MCP4922 VSS (Pin 12) - all MCP4922 have this connection in common
  • Teensy 5V or 3.3V (depending on model)to MCP4922 V REF B (Pin 11) - all MCP4922 have this connection in common
  • Teensy 5V or 3.3V (depending on model) to MCP4922 SHDN - all MCP4922 have this connection in common
  • Teensy ground to MCP4922 LDAC - all MCP4922 have this connection in common
  • MCP4922 V OUT A (Pin 14) is the output for DAC A - all MCP4922 have this connection in common
  • MCP4922 V OUT B (Pin 10) is the output for DACB - all MCP4922 have this connection in common
Almost all connections are duplicated for each MCP4922 - with the exception of the CS (Chip Select) pin, which requires a unique digital pin on the Teensy for each MCP4922.

This way, only a particular MCP4922 is selected when data is written to the SPI bus.





Software
The SPI library can be used to easily write data to the DAC. In the example below,  a function is written that has a chip select pin value, a DAC channel value and a DAC output value between 0 - 4095. This function writes to the SPI bus and updates the DAC output.

This function is then connected to MIDI via MIDI control messages.

This particular sketch supports both high res MIDI and normal res MIDI. The controller number that is used determines the DAC channel number as well as the MCP4922 chip.

In this example, the mapping is as follows:
  • CC 0 paired with CC 32 will send a high resolution value to MCP4922 chip 1 DAC 0
  • CC 1 paired with CC 33 will send a high resolution value to MCP4922 chip 1 DAC 1
  • CC 2 paired with CC 34 will send a high resolution value to MCP4922 chip 2 DAC 0
  • CC 3 paired with CC 35 will send a high resolution value to MCP4922 chip 2 DAC 1
  • CC 64 will send a normal resolution value to MCP4922 chip 1 DAC 0
  • CC 65 will send a normal resolution value to MCP4922 chip 1 DAC1
  • CC 66 will send a normal resolution value to MCP4922 chip 2 DAC 0
  • CC 67 will send a normal resolution value to MCP4922 chip 2 DAC 1
The mapping is arbitrary, to some degree - so adjust as desired.

Download here: http://milkcrate.com.au/_other/downloads/arduino/MCP4922_DAC_MULTI_MIDI/





MIDI-controlled 12-bit DAC

Overview
The MCP4922 is a 12-bit, two channel, SPI-based DAC - and can be easily connect to a studio setup using a Teensy via MIDI!This is an easy way of adding a 0 - 5V or 0 - 3.3V 4096-step DAC to a MIDI sequencing environment.

The cost of these chips is relatively low - around AUD$3 per chip or $1.50 per DAC channel - and they don't require any additional components for basic implementation.  



Hardware
Hardware connection is identical to the previous post on the MCP4922. To connect the MCP4922 to a Teensy microcontroller, the following connections should be made:

  • Teensy 5V or 3.3V (depending on model) to MCP4922 VDD (Pin 1)
  • Teensy SPI Chip Select pin (either user-designated or as defined in the SPI library) to MCP4922 CS (Pin 3)
  • Teensy SPI Serial Clock pin (as defined in the SPI library) to MCP4922 SCK (Pin 4)
  • Teensy SPI Digital Out (as defined in the SPI library) to MCP4922 SDI (Pin 5)
  • Teensy 5V or 3.3V (depending on model) to MCP4922 V REF A (Pin 13)
  • Teensy ground to MCP4922 VSS (Pin 12)
  • Teensy 5V or 3.3V (depending on model)to MCP4922 V REF B (Pin 11)
  • Teensy 5V or 3.3V (depending on model) to MCP4922 SHDN
  • Teensy ground to MCP4922 LDAC
  • MCP4922 V OUT A (Pin 14) is the output for DAC A
  • MCP4922 V OUT B (Pin 10) is the output for DACB





Software
The SPI library can be used to easily write data to the DAC. In the example below,  a function is written that has a chip select pin value, a DAC channel value and a DAC output value between 0 - 4095. This function writes to the SPI bus and updates the DAC output.

This function is then connected to MIDI via MIDI control messages.

This particular sketch supports both high res MIDI and normal res MIDI. 

Download here: http://milkcrate.com.au/_other/downloads/arduino/MCP4922_DAC_MIDI/





Teensy and MCP4922 Dual Channel 12-Bit DAC

Overview
The MCP4922 is a 12-bit, two channel, SPI-based DAC. As microcontrollers do not often have many DAC outputs (and are often limited to PWM outputs), cheap external DACs are a great way of increasing the analog output options of a microcontroller such as the Teensy.

The cost of these chips is relatively low - around AUD$3 per chip or $1.50 per DAC channel - and they don't require any additional components for basic implementation. 


Hardware
To connect the MCP4922 to a Teensy microcontroller, the following connections should be made:

  • Teensy 5V or 3.3V (depending on model) to MCP4922 VDD (Pin 1)
  • Teensy SPI Chip Select pin (either user-designated or as defined in the SPI library) to MCP4922 CS (Pin 3)
  • Teensy SPI Serial Clock pin (as defined in the SPI library) to MCP4922 SCK (Pin 4)
  • Teensy SPI Digital Out (as defined in the SPI library) to MCP4922 SDI (Pin 5)
  • Teensy 5V or 3.3V (depending on model) to MCP4922 V REF A (Pin 13)
  • Teensy ground to MCP4922 VSS (Pin 12)
  • Teensy 5V or 3.3V (depending on model)to MCP4922 V REF B (Pin 11)
  • Teensy 5V or 3.3V (depending on model) to MCP4922 SHDN
  • Teensy ground to MCP4922 LDAC
  • MCP4922 V OUT A (Pin 14) is the output for DAC A
  • MCP4922 V OUT B (Pin 10) is the output for DACB





Software
The SPI library can be used to easily write data to the DAC. In the example below,  a function is written that has a chip select pin value, a DAC channel value and a DAC output value between 0 - 4095. This function writes to the SPI bus and updates the DAC output.

Download here: http://milkcrate.com.au/_other/downloads/arduino/MCP4922_DAC/




Sunday, January 13, 2013

Using an SD Card Sniffer to Hear an SPI Clock Signal

I used an SD card sniffer to listen to the SPI clock signal of transferring data to and from the SD card.