How to use analogRead() in riot for esp8266 board and ldr sensor

Hi, I am new to Riot and a beginner in programming. I am doing an experiment on LDR sensor with Esp8266 board for reading captured data from a basic ldr sensor (the one from the arduino beginners kit). I am working on XUbuntu 20.04.4 LTS x86_64.

So far I just want to stdout the data in terminal, the long term idea is to send this information as a UDP message.

If there is a canonical way to address this need, could anybody point it out for me?

The original arduino code i am trying to replicate is the following

==Arduino==


const int ldrPin = A0;
int lastRead = 0;

void setup() {
  Serial.begin(9600);
  pinMode(ldrPin, INPUT);
}
void loop() {
  int ldrStatus = analogRead(ldrPin);
  int diff = ldrStatus - lastRead;
  if (!(dif > -5 && dif < 5)) {
    lastRead = ldrStatus;
    Serial.println(ldrStatus);
  }
  delay(2000);
}

==================================

Below the main two approaches I have tried so far:

Using GPIO

For accomplishing I firstly tried to do it by following Getting Started with Arduino and RIOT-OS: Setting up RIOT-OS on Arduino.

After modyfing the Hello world example I end up with this code and without modifying its Makefile.

===== Makefile =====

USEMODULE += esp_wifi
USEMODULE += gnrc_sock
USEMODULE += gnrc_sock_check_reuse
USEMODULE += gnrc_sock_udp
USEMODULE += gnrc_ipv6

===== main.c =====

#include <stdio.h>
#include "periph/gpio.h" 
#define ldrPin GPIO_PIN(2,0)
int main(void)
{
   gpio_init (ldrPin, GPIO_IN);
   while(1) {
    puts("Light");
	int ldrStatus = gpio_read(ldrPin);
    printf(" %d MCU.\n", ldrStatus);
    
    }
    return 0;
}

Note please that GPIO_PIN(2,0) value I guessed from the example. I am trying to point to the Analogic 0 pin.

This program compiles and run, but it returns only 0/1, it’s not what i need. I would like to get the same kind of readings as with Arduino IDE.

I suspected I should specify the fact that is analog pin. I found GPIO_IN_ANALOG in the documentation (GPIO β€” RIOT 0.1.1 documentation), but when using this enum value the program does not compile anymore. I checked in the RIOT/drivers/include/periph/gpio.h, and I found out that the enum value GPIO_IN_ANALOG is not defined and I cannot find any replacement for analog readings.

Using Arduino

In this trial I did not reach that far. I added arduino module to the same makefile I used arduino functions pinMode and analogRead in a simple main function. But it would not compile.

===== Makefile =====

USEMODULE += esp_wifi
USEMODULE += arduino
USEMODULE += gnrc_sock
USEMODULE += gnrc_sock_check_reuse
USEMODULE += gnrc_sock_udp
USEMODULE += gnrc_ipv6

===== main.c =====

#include <stdio.h>
#include "Arduino.h" 

#define ldrPin A0
int main(void)
{
	pinMode(ldrPin, INPUT);
   while(1) {
    puts("Light");
	int ldrStatus = analogRead(ldrPin);
    printf(" %d MCU.\n", ldrStatus);
    
}
    return 0;
}

Any idea how I can use the arduino API in Riot? or just to make an equivalent code to work with riot?

Thank you very much!

Just in case you are still on it I will throw my 2 cents in.

I am guessing you actually want an analog readings from the pin not a 1 or 0. The esp8266 arduino pin map does not seem to have an A0 which is why it may fail to compile. It seems the esp8266 has one ADC available. You could start with running an ADC test to at least get some readings.

1 Like

Thanks for your response. I found the way to config ADC fur analog read in the RIOT repository. It is working now.