Microsoft MakeCode

MCP41050 SPI Makecode Block problem

Hi Please can you help.

I wish to use a Micro:bit in Makecode Block to control an MCP41050 to give a Variable Resistance. We have tried everything we can using all the options that we could find. Although these were limited to Mode, Frequency, Chip Select, MOSI, MISO, SCK.

The MCP41050 does not require the MISO as it only requires a signal from the Micro:bits SPI Pin to set it’s desired Resistance value.

Could you tell me how to get this to operate, as all we seem to find is “set the SPI Output to read back the value from the SPI Device”?

Kind regards

Tim

I believe that the SPI is on pins 13, 14, 15:

You should be able to write a value using the “analog write pin” block

Oh, never mind, we actually have a block for this:

It’s under the “more” subcategory of pins

Hi @richard I have my Pins on P1 Chip Select, P13 SCK, P14 MISO, and MOSI is not used on the MCP41050 so there nothing connecting it to MB I tried 5V and 3V to the MCP41050 And my code uses SPI write but Im Confused of how the block works, as its not like Analog Write Block.

But I don’t understand “Write a data value to the SPI slave device and return its response” as there is no SPI returned data on this SPI write only Chip?

All my attempts to send a data value to the SPI make no difference to the device. The MCP41050 is an SPI chip that provides a “real” Resistance in Ohms when an appropriate SPI data value is applied. 90 to 255)

The Resistance between either end of the Potentiometer and the Wiper show approximately 23Kohm ie half the Resistance of the device.

So I am not too sure what I am doing wrong?

Tim

Disclaimer: I have zero experience with SPI and I apologize if this is nonsense (more of a software guy than a hardware guy) but i’m going to try my best to help!

Looking at the data sheet for that chip, it looks like you need to write two bytes: a command byte and a data byte

With that in mind, there is a javascript-only transfer function that lets you send multiple bytes at once. Here is some sample code that might help (not tested at all):

// Command byte
// Send write command to both potentiometers
const command = 0b00010011

// Data to write
const data = 128;

// Pack the two bytes into a buffer
const message = pins.createBuffer(2);
message.setUint8(0, command);
message.setUint8(1, data);

// Not sure if below function required or not. First argument is the
// number of bits (16 = 2 bytes) and the second argument is
// the SPI mode. The data sheet mentions that the chip
// supports modes 0 and 3

// pins.spiFormat(16, 0);

// Do the actual send. Pass null for the response buffer because
// we aren't expecting one
pins.spiTransfer(message, null);

Hi @richard I tend to use MakeCode Block, but I can follow a bit of Java. So I have tried importing your Java code into MakeCode Block, and it appears to be happy with it.

I will have a try tomorrow at controlling the MCP41050 and let you know how I get on with it. Or try your Java Code directly. I will let you know how I get on with it.

Does this mean that perhaps the SPI MakeCode Blocks could do with an update to accomodate this type of chip or use?

What I am looking at doing is controlling my Scalextric Digital with a Micro:bit. That is why I need a “pure” Resistance Potentiometer of 0 to 18 Kohm. The MCP41050 works fine on the Scalextric when controlled by an Arduino, which my brother programmed, but I wanted to use the Micro:bit which I can program by myself.

Tim

hi @richard read through the Java Code again, and it says “Send write command to both potentiometers”. I am using the MCP41xxx chip which is a single Potentiometer. The MCP42xxx is a dual Potentiometer.

I am not too sute about Chip Select, as it may not be required for only one I.C. being CS, and I can try strapping it up or down? But the Scalextric has 4 cars that can be controlled, and so I was going to use 4 of MCP41050 I.C.s, one for each car.

I will see what results I get

Tim

Yeah, i expect it will work similarly either way, but you can always try changing the bits in the command byte to the single potentiometer option.