Analog pin reading

Working libraries, libraries being ported and related hardware
Post Reply
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Analog pin reading

Post by mebab »

Sorry if my question looks so simple to you!
I have been trying to read analog values connected to the pin PC4 in STM32L476. When I change values (0 - 3.3 V), I just can see either 0 or 1023. It cannot show equivalent integer values for analog voltages as they are! I use Arduino IDE and have define pinMode (PC4, INPUT) and use analogRead(PC4) to read the pin voltage. It seems that it considers the pin as a digital pin.
What is wrong with this simple task?

Thanks in advance,
by fpiSTM » Wed Jun 02, 2021 4:02 pm
What is your VREF on your boards ?
Any schematics ?
Go to full post
Last edited by mebab on Wed Jun 02, 2021 12:57 pm, edited 1 time in total.
mlundin
Posts: 94
Joined: Wed Nov 04, 2020 1:20 pm
Answers: 6
Location: Sweden

Re: Analog pin reading

Post by mlundin »

Should work, can you show circuit diagram and complete code ?
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: Analog pin reading

Post by mebab »

Thanks Mlundin,
I use ST-Link to program my board. Therefore, to see the results, I use LoRa simple node example to send the value of reading to a receiver to read it. The following is the code for the node:

Code: Select all

#include <SPI.h>              // include libraries
#include <LoRa.h>

const long frequency = 433E6;  // LoRa Frequency

const int CS_LoRa = PC7;          // Pin 38 - LoRa radio chip select
const int resetPin = PB8;        // Pin 61 - LoRa radio reset; This is 9 in Library .h file
const int irqPin = PA10;          // change for your board; must be a hardware interrupt pin

void setup() {
  Serial.begin(115200);                   // initialize serial
  while (!Serial);

  LoRa.setPins(CS_LoRa, resetPin, irqPin);

  if (!LoRa.begin(frequency)) {
    Serial.println("LoRa init failed. Check your connections.");
    while (true);                       // if failed, do nothing
  }

  Serial.println("LoRa init succeeded.");
  Serial.println();
  Serial.println("LoRa Simple Node");
  Serial.println("Only receive messages from gateways");
  Serial.println("Tx: invertIQ disable");
  Serial.println("Rx: invertIQ enable");
  Serial.println();

  LoRa.onReceive(onReceive);
  LoRa.onTxDone(onTxDone);
  LoRa_rxMode();
//---------------------------------------------------------------------------------------------

  pinMode(PC4, INPUT);

}
void loop() {

  if (runEvery(3000)) { // repeat every 1000 millis

    String message = "Vb =  ";
    int VB= analogRead(PC4);  
    message += VB;
    LoRa_txMode();
    LoRa_sendMessage(message); // send a message

  }
    delay(1000);
}



void LoRa_rxMode(){
  LoRa.enableInvertIQ();                // active invert I and Q signals
  LoRa.receive();                       // set receive mode
}

void LoRa_txMode(){
  LoRa.idle();                          // set standby mode
  LoRa.disableInvertIQ();               // normal mode
}

void LoRa_sendMessage(String message) {
  LoRa_txMode();                        // set tx mode
  LoRa.beginPacket();                   // start packet
  LoRa.print(message);                  // add payload
  LoRa.endPacket(true);                 // finish packet and send it
}

void onReceive(int packetSize) {
  String message = "";

  while (LoRa.available()) {
    message += (char)LoRa.read();
  }

  Serial.print("Node Receive: ");
  Serial.println(message);
}

void onTxDone() {
  Serial.println("TxDone");
  LoRa_rxMode();
}

boolean runEvery(unsigned long interval)
{
  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    return true;
  }
  return false;
}
and this is the receiver:

Code: Select all

#include <SPI.h>              // include libraries
#include <LoRa.h>

const long frequency = 433E6;  // LoRa Frequency
const int CS_LoRa = 10;          // LoRa radio chip select
const int resetPin = 3;        // LoRa radio reset; This is 9 in Library .h file
const int irqPin = 2;          // change for your board; must be a hardware interrupt pin

void setup() {
  Serial.begin(115200);                   // initialize serial
  while (!Serial);

  LoRa.setPins(CS_LoRa, resetPin, irqPin);

  if (!LoRa.begin(frequency)) {
    Serial.println("LoRa init failed. Check your connections.");
    while (true);                       // if failed, do nothing
  }

  Serial.println("LoRa init succeeded.");
  Serial.println();
  Serial.println("LoRa Simple Gateway");
  Serial.println("Only receive messages from nodes");
  Serial.println("Tx: invertIQ enable");
  Serial.println("Rx: invertIQ disable");
  Serial.println();

  LoRa.onReceive(onReceive);
  LoRa.onTxDone(onTxDone);
  LoRa_rxMode();
}

void loop() {
  if (runEvery(5000)) { // repeat every 5000 millis

    String message = "HeLoRa World! ";
    message += "I'm a Gateway! ";
    message += millis();

    LoRa_sendMessage(message); // send a message

    Serial.println("Send Message!");
  }
}

void LoRa_rxMode(){
  LoRa.disableInvertIQ();               // normal mode
  LoRa.receive();                       // set receive mode
}

void LoRa_txMode(){
  LoRa.idle();                          // set standby mode
  LoRa.enableInvertIQ();                // active invert I and Q signals
}

void LoRa_sendMessage(String message) {
  LoRa_txMode();                        // set tx mode
  LoRa.beginPacket();                   // start packet
  LoRa.print(message);                  // add payload
  LoRa.endPacket(true);                 // finish packet and send it
}

void onReceive(int packetSize) {
  String message = "";

  while (LoRa.available()) {
    message += (char)LoRa.read();
  }

  Serial.print("Gateway Receive: ");
  Serial.println(message);
}

void onTxDone() {
  Serial.println("TxDone");
  LoRa_rxMode();
}

boolean runEvery(unsigned long interval)
{
  static unsigned long previousMillis = 0;
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval)
  {
    previousMillis = currentMillis;
    return true;
  }
  return false;
}
About the circuit diagram, I just connect a potentiometer to a voltage source to change the measured voltage of the PC4.
This is what gateway receives (0 or 1023):

Code: Select all

Gateway Receive: Vb =  1023
mlundin
Posts: 94
Joined: Wed Nov 04, 2020 1:20 pm
Answers: 6
Location: Sweden

Re: Analog pin reading

Post by mlundin »

I am using a Nucleo 64 L476RG card, connects the A3 pin, dac out, to PC4.
STM32dino v2, select board Nucleo 64 and partnumber Nucleo L476RG
Arduino serial monitor

pinMode(PC4, INPUT); is not needed, analogRead does the pin configuration.

check the input voltage with a multimeter.

Following code works:

Code: Select all

void setup() {
  // put your setup code here, to run once:
  delay(20);
  Serial.begin(115200);
  while (!Serial);
  delay(500);
  Serial.println();
  Serial.print("SETUP: ");
  Serial.println(BOARD_NAME);
  Serial.print("FLASH->ACR = 0x");Serial.println(FLASH->ACR,HEX); 
  Serial.print("SystemCoreClock: ");
  Serial.println(SystemCoreClock);
  Serial.print("F_CPU: ");
  Serial.println(F_CPU);
  Serial.printf("RCC_CR 0x%08X \n", RCC->CR);
  Serial.printf("RCC_CFGR 0x%08X \n", RCC->CFGR);
  Serial.printf("RCC_PLLCFGR 0x%08X \n", RCC->PLLCFGR);

  pinMode(PC4, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  static int outval = 0;
  analogWrite(A2,outval); /* full range 0-255 */
  outval += 10;
  if (outval > 255) outval = 0;
  delay(10);
    String message = "Vb =  ";
    int VB= analogRead(PC4);  /* full range 0-1024 */
    message += VB;
  Serial.println(analogRead(PC4));
  Serial.println(message);
  delay(1000);
}
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Analog pin reading

Post by fpiSTM »

What is exactly your board?
And which target you selected in the menu ? Nucleo L476 or generic L476 ? Tested both and it works fine.

Try a simple example like analogReadSerial example by replacing A0 by PC4 (note that in the example you do not need pinmode).
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: Analog pin reading

Post by mebab »

I also have successfully tested Nucleo 64 L476RG card. It works fine.
However, I have developed several boards using STM32L476. I use generic L476 and there is no problem with other tasks like SPI, I2C. etc,
The problem arises with reading analog input values on all boards as I mentioned and I cannot figure out the reason!

I did another test while I set PC4 as output and I could change it via the code which means the connection is correct.

Do I have to apply any settings to ADC? I ask this because it looks like it doesn't consider the pin as Analog input and just shows the minimum (0) and maximum (1023) of 10-bit digital values!
mlundin
Posts: 94
Joined: Wed Nov 04, 2020 1:20 pm
Answers: 6
Location: Sweden

Re: Analog pin reading

Post by mlundin »

Problem with analog inputs on this specific pin PC4 or on all analog input pins ?

The settings to the ADC is handled by the analogRead function.
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: Analog pin reading

Post by mebab »

I just tested. The same problem also with other analog pins!
for analog voltage:
Vin < 1 volt, gives 0
Vin >1 volt, gives 1023
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Analog pin reading

Post by fpiSTM »

What is your VREF on your boards ?
Any schematics ?
mebab
Posts: 115
Joined: Mon Aug 24, 2020 12:42 pm
Answers: 4

Re: Analog pin reading

Post by mebab »

fpiSTM wrote: Wed Jun 02, 2021 4:02 pm What is your VREF on your boards ?
Any schematics ?
Thank you fpiSTM!
I don't have external connections to these pins (Vref+ and Vref-). However, it looks like that they are internally connected to Vcc and GND.

I tried to externally connect Vref+ to Vcc and seems that problem is solved now. Then, I think fpiSTM addressed the right point!
Post Reply

Return to “Libraries & Hardware”