changing DigitalOut PinName

18 Oct 2010

I would like to know if there is a way to a change the PinName after you created a DigitalOut object.


DigitalOut Led(p21);


18 Oct 2010

So you want to decide that Led is now the item connected to p22 rather than p21?

To do that you'd just do:

DigitalOut Led(p21);

DigitalOut Led(p22);

18 Oct 2010 . Edited: 18 Oct 2010

I appreciate you answered me. It is more complicated. I wanna modified the ADXL345 library to be able to change the nCS_ variable because I have multiple devices connected to the same SPI bus.


ADXL345(PinName mosi, PinName miso, PinName sck, PinName cs);

The CS is store in a private variable:


SPI        spi_;
DigitalOut nCS_;

Constructor function:

ADXL345::ADXL345(PinName mosi,
PinName miso,
PinName sck,
PinName cs) : spi_(mosi, miso, sck), nCS_(cs) {

//2MHz, allowing us to use the fastest data rates.

nCS_ = 1;



I tried your option but it did not work:
void ADXL345::setChipSelect(PinName cs){

DigitalOut nCS_(cs);


I tried this one, too.
void ADXL345::setChipSelect(DigitalOut cs){



I need to change nCS_ with a new pin value. Any other suggestion? 

18 Oct 2010

Hi Jose,

The pin is assigned in the constructor, and constant for the lifetime of the object. One solution is therefore to dynamically create the DigitalOut objects:

#include "mbed.h"

int main() {
    DigitalOut *d;
    d = new DigitalOut(LED1);
    delete d;
    d = new DigitalOut(LED2);
However, before you rush out and start doing that, I think there much be a much more elegant approach to what you are trying to do. If I understand correctly, you are basically using a SPI bus with multiple devices connected. This is a pretty normal setup, and can be handled nicely.

The SPI interface allows you to have multiple virtual instances using the same physical interface. This means you can have a SPI interface associated with each object, and a different DigitalOut controlling the CS. So, for example:

#include "mbed.h"

SPI spi1(p5,p6,p7);  
DigitalOut cs1(p8); 

SPI spi2(p5,p6,p7);  
DigitalOut cs2(p9);

int main() {
   cs1 = 1;
   cs2 = 1;


   // write to device 1
   cs1 = 0; 
   cs1 = 1; 

   // write to device 2   
   cs2 = 0; 
   cs2 = 1; 
So in the case of your accelerometer, lets say you had two connected to the same bus, each with a different DigitalOut cs. You could (without modification to the AXDL345 code) do the following:

#include "mbed.h"
#include "AXDL345.h"

AXDL345 a1(p5, p6, p7, p8); // cs on p8
AXDL345 a2(p5, p6, p7, p9); // cs on p9

int main() {
    // ...

Importantly, each library component doesn't need to know about the other library components sharing the same bus! This is really useful, and means you don't need to modify the library to start connecting up multiple things. I think this may actually give you what you are after for free!

Hope this helps,


18 Oct 2010

Hi Simon,

Thank you very much for let me know about the SPI multiple virtual instances.  I tried your suggestion and It solved my issue! I started the post because I wonder how difficult  to add a new function to the DigitalOut object could be. Usually,  Objects can be modified after you created them using functions. So far, DigitalOut inlcudes write and read only. Is it possible to add a new function to that object?. Best regards,


29 Sep 2016

What about the memory? Doesn't double instances use double memory?