hello,
i am wanting to control the resistance of a digital potentiometer through bela using pure data. wondering if it is possible, and what connections - analog out is limited on my bela mini multichannel (i believe?), but i also have a standard bela with face, cape, and multiplexer to play with...
as an aside, i have made a successful project using bela mini and multichannel - its SO GOOD TO USE - running 6 channels of live pure data effects and no compiling nightmares or any issues at all, except with my own idiotic patching. i will post pics and sound when the exhibition comes down. so i am understanding some bela basics, obviously, but want to expand my possibilties of controlling external analog electronics - thus the digipot. any other options for controlling resistance in an external analog circuit (up to about 12v, resistance dynamically set using lfos from within pure data, etc) - any suggestions welcome!
thanks!
a simpleton asking about digital pots....
Can you post the datasheet of the part you want to use?
- Edited
....
- Edited
also, to be clear (and add complexity), i will be using my bela with face and cape and multiplexer stack (i have a bela mini multichannel also). i am trying to get 4-in 8-out audio, lots of pots to control 4 effects in pd patches, and 3 digipots to interface with hardware and control the voltage level of 3 neon oscillators (digipots will only be subjected to 5.5v max)....
i've been trying to wrap my head around digipots, and work out my exact requirements. SPI seems easier, but i am still in the dark about learning how and implementing it....
i assume digipots would be best, but there may be something easier - i am trying to replace a 50k pot that controls the voltage of a booster going out to neon oscillators - the pot is subjected to 5.5v maximum across its terminals. i want to be able to control the voltage going to the neon oscillators from bela and pure data (and vary their pitch and beat, using lfos etc....)
sorry for all the repeated posts as i try to work out what to do...!
- Edited
one last thing - a split rail digipot with higher voltage rating may make more sense if this is at all feasible....
.
MCP41HV
.
- Edited
How many of these do you want to run and how often do you want to update them? They use an SPI connection, which means:
- one CS line to address the device (one GPIO per device)
- one SCK ine to drive a clock (shared across devices)
- one SDO line to drive data (shared across devices)
- optionally, one SDI line to read data (shared across devices)
You can either use the SPI peripheral on BelaMini - with a GPIO dedicated to each device or bitbang the protocol using the Bela digital channels (accessed via digitalWriteOnce()
/digitalRead()
from the real-time render()
callback. The bitbanged version will be slower, but its timing will be predictably coupled to the audio processing.
For using the SPI peripheral, see example Communication/SPI. For using the bitbanged version you can use the ShiftRegister class as a starting point, see the example Sensors/shift-register-out. In this case, reading is more complicated and the easiest way is to use the ShiftRegisterIn
class by looping back the SCK and CS lines between the ShiftRegisterOut
and ShiftRegisterIn
.
Btw, I am suggesting to do this from C++ first and then move on to PureData.
You'll want to look at the actual datasheet - the one you posted was truncated I believe? - https://ww1.microchip.com/downloads/en/DeviceDoc/20005207B.pdf
As for what data to write, you if that's hard to parse from the datasheet, you can take inspiration from the arduino library: https://github.com/gregsrabian/MCP41HVX1/blob/master/MCP41HVX1.cpp#L130C3-L156C2
If using the SPI class (as per the Communication/SPI example), the below modified example should do an increment every half second.
/*
____ _____ _ _
| __ )| ____| | / \
| _ \| _| | | / _ \
| |_) | |___| |___ / ___ \
|____/|_____|_____/_/ \_\
https://bela.io
*/
/**
\example Communication/Spi/render.cpp
Reading a SPI device on the Bela Mini
-------------------------
This sketch initialises the second SPI device (SPI1) on the Bela Mini, and reads data from it.
The SPI pins are:
P2-25: MOSI
P2-27: MISO
P2-29: CLK
P2-31: CS
To enable these pins for SPI you must first run the following commands (e.g.:
in the console at the bottom of the Bela IDE, or via `ssh`)
config-pin P2.25 spi
config-pin P2.27 spi
config-pin P2.29 spi_sclk
config-pin P2.31 spi_cs
The SPI readouts are then performed in an Auxiliary Task, seperate from the real-time audio thread.
*/
/*
MIT License
Copyright (c) 2020 Jeremiah Rose
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <Bela.h>
#include <stdio.h>
#include <libraries/Spi/Spi.h>
// Create a seperate thread to poll Spi from
AuxiliaryTask SpiTask;
void readSpi(void*); // Function to be called by the thread
float readInterval = 0.5;// How often readSpi() is run (in seconds)
int readCount = 0; // Used for scheduling, do not change
int readIntervalSamples; // How many samples between, do not change
Spi spiDevice;
unsigned char exampleOutput;
bool setup(BelaContext *context, void *userData)
{
spiDevice.setup({
.device = "/dev/spidev2.1", // Device to open
.speed = 500000, // Clock speed in Hz
.delay = 0, // Delay after last transfer before deselecting the device
.numBits = 8, // No. of bits per transaction word
.mode = Spi::MODE0 // Spi mode
});
// Set up auxiliary task to read Spi outside of the real-time audio thread:
SpiTask = Bela_createAuxiliaryTask(readSpi, 50, "bela-Spi");
readIntervalSamples = context->audioSampleRate * readInterval;
return true;
}
// Runs every audio frame
void render(BelaContext *context, void *userData)
{
// Runs every audio sample
for(unsigned int n = 0; n < context->audioFrames; n++) {
// Schedule auxiliary task for Spi readings
if(++readCount >= readIntervalSamples) {
readCount = 0;
Bela_scheduleAuxiliaryTask(SpiTask);
}
// use Spi output for whatever you need here
// exampleCalculation = (int) exampleOutput + 1;
}
}
// Auxiliary task to read Spi
void readSpi(void*)
{
const int transmissionLength = 1; // Number of bytes to send/receive
unsigned char Tx[transmissionLength]; // Buffer to send
unsigned char Rx[transmissionLength]; // Buffer to receive into
// set the data to send
Tx[0] = 0x04; // command: increment
//Tx[0] = 0x08; // command: decrement
if (spiDevice.transfer(Tx, Rx, transmissionLength) == 0)
{
// Print result
printf("Spi: Transaction Complete. Sent %d bytes, received: ", transmissionLength);
int n = 0;
for(n = 0; n < transmissionLength; ++n)
printf("%#02x ", Rx[n]);
printf("\n");
// Process received buffer. In this example we just send the first byte
// to the audio thread via the global variable exampleOutput
exampleOutput = Rx[0];
}
else
fprintf(stderr, "Spi: Transaction Failed\r\n");
}
void cleanup(BelaContext *context, void *userData)
{
}
Uncomment the decrement
line instead to make it a decrement every half second.
If you want to set a specific value, set const int transmissionLength = 2
and use these lines instead of the increment/decrement ones:
// set the data to send
Tx[0] = 0x00; // command: set position
Tx[1] = 123; // this is the value that you set the pot to (max is 255)
- Edited
giulio,
thanks so much for the detailed possibilities. its very much appreciated.
but, i am not yet that good that i think i can pull this off without endless help - my c++ programming skills are infantile. so i am considering a simpler solution, even if it is a little limited....
do you think it would be easy to use a simple up-down digipot and program it all from within pure data - to send out up/down and increment signals on digital outs without having to leave the safe shores of pure data...? i will eventually get better at coding but right now there are too many unknowns for me to make any headway. below is the chip i am imaging using (i have soldered smd, but not yet this fine - will need to practice....)
i imagine i can assign pins and set up everything within pure data following the fundamentals laid out in the bela pure data digital output guide
i only need to control 3 variable resistors, so the bela digital pins would be plenty. and they dont need to change value super fast - i am imagining pure data lfos that change the digipot resistance at a rate of something like a full second or more across the 128 steps. if that makes sense. i don't care if this is a clunky solution as long as it is possible to make it work fairly easily.
thanks, and apologies i am not yet up to speed with what is required for what you suggested!
mrseanoconnell do you think it would be easy to use a simple up-down digipot and program it all from within pure data - to send out up/down and increment signals on digital outs without having to leave the safe shores of pure data...? i w
it will be possible, but first try the example above according to my instructions and see if it works.
giulio,
thanks - i have parts coming and will try to muddle through! will post when i get somewhere.