cancel
Showing results for 
Search instead for 
Did you mean: 

Wb55 - How to read values from ADC

JDIRI.1
Associate II

Hi everybody!

Working on Martian Robot développed by VittaScience​, I need to read values coming from 5 blackline sensors to the ADC of the wb55.

I tried to analogRead() the pin but nothing...

Some ideas?

Thanks!​

1 ACCEPTED SOLUTION

Accepted Solutions
Jaroslav JANOS
ST Employee

Just tested on 500Kbit/s, works fine for me. Please check the wiring and SPI configuration: master mode, full duplex, datasize 10 bit and enabled NSS signal on PA4.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

11 REPLIES 11
Javier1
Principal

There is a lot of context missing here,

Are you using Arduino? If you are, try posting here also https://www.stm32duino.com/

we dont need to firmware by ourselves, lets talk
Jaroslav JANOS
ST Employee

Hi @JDIRI.1​ ,

the black line sensors on the Martian robot are connected to an external ADC, which is read by MCU via SPI. Now, as @Javier Muñoz​ mentioned, it is not clear, which analogRead() function you are referring to. Is it Arduino? Or Python, and you are referring to the analogRead() function from the Alphabot library? Anyway, I would recommend you to use the programming interface of Vittascience...

BR,

Jaroslav

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

JDIRI.1
Associate II

Thank you for your answers @Javier Muñoz​ and @Jaroslav JANOS​ 

@Jaroslav JANOS​ we are in STM WB55 board and used HAL_READ_PIN() to read the values.

Do you mean we should use HAL_SPI_Receive() to read the values?

The problem is we don't know which sensor give his value..

Jaroslav JANOS
ST Employee

Unfortunately, I do not have the schematics of the robot and cannot test it right now, but I doubt that you read any meaningful values by HAL_Read_Pin(). Which pin were you reading? From their code and images, it appears to me, that the outputs of the sensors are connected only to the external ADC.

From the code it also appears to me, that MISO and MOSI pins are swapped on the robot, so you also cannot use the SPI interface and have to emulate it by software ("manually" switching GPIOs) - same way Vittascience does. See the Python code.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

JDIRI.1
Associate II

Thank you for your answer @Jaroslav JANOS​.

Have you some examples about reading in SPI please?

Thank you

Jaroslav JANOS
ST Employee

You can find some examples of SPI usage (using HAL) e. g. here. But as I mentioned, you unfortunately cannot use it for the robot.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

JDIRI.1
Associate II

Thank you @Jaroslav JANOS​, we just modified the pinout of the shield of the Robot by connect D11 to D12 and D12 to D11.

By now, we should could use the SPI libraries... right?

Thank you very much!

Jaroslav JANOS
ST Employee

Yes, in that case you can use those. Having configured SPI1 for full duplex communication with 10 bit data size and low baud rate, you can then read the sensors like this:

int16_t input_id;
uint8_t iter;
uint16_t rxBuf, txBuf;
 
for (iter = 0; iter <= NUM_SENSORS; ++iter) {
	input_id = MAX(iter - 1,0);
 
	txBuf = iter << 6;
 
	HAL_SPI_TransmitReceive(&hspi1, (uint8_t *)&txBuf, (uint8_t *)&rxBuf, 1, 1000);
	values[input_id] = rxBuf & 0x3FF;
 
	HAL_Delay(1);
}

Note that this code can be further optimized, e. g. the delay might be shortened to 22 us.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

JDIRI.1
Associate II

@Jaroslav JANOS​ I just tried, with baudrate = 500Kbits/s

I read 1023 all the time for all sensors, in dark or light....

I replaced : input_id = MAX(iter - 1,0);

with : if (iter != 0)

 input_id = iter - 1;

  else

  input_id = 0;

and declared values :

 uint16_t values[MAX_SENSORS];

sounds correct?