C/C++ on Intel Galielo/Edison: Part6 Light sensor

In this post, we will be using the upm library to interface the  Grove Light sensor
with the Galileo/Edison using upm on C++.

The light sensor is made up of primarily of LDR and opamp:


“LDR” stands for light dependent resistor (in above schematic, it is marked as “LIGHT”) as the name suggests, it is made up of a material that changes it resistance depending on the intensity of the ambient light.

Normally, the resistance decreases when the ambient light intensity increases. The operational amplifier (op amp) in the Light sensor Grove module is configured in voltage follower mode. Which means that the opamp will output whatever voltage is applied at the “+” input. This helps us yo have the proper signal strength at the ADC pin. The LDR along with resistor R1 forms the voltage divider circuit applied at “+” input. The LDR is connected between Vcc and the rest of the voltage divider circuit. Now with the varying resistance of the LDR (with light intensity) which is part of the voltage divider circuit, the current flowing through LDR and hence the voltage at point “+” changes (in accordance to ohm’s law), this is output by the opamp and this is what will be measured by the Galileo/Edison using ADC.

Hardware connections

Connect the light sensor to port A0 and LED to port D5 either using the Grove LED module or on a breadboard via the current limiting resistor as shown in the schematic.


The idea here is to control the intensity of the LED connected to port D5 using the light sensor


Download the source from github onto your Galileo/Edison

curl https://raw.githubusercontent.com/navin-bhaskar/C-CPP-on-Intel-Edison-Galileo/master/part6-light_sensor/light_sensor.cpp > light_sensor.cpp

Compile the code using following command:

g++ light_sensor.cpp -o light_sensor -lupm-grove -lmraa

Run the example using following command


Vary the intensity on the light sensor by using a torch or flash light from your phone to see the intensity of the LED connected to port D5 chnage and also the vertical bar displayed on the console should vary in accordance to the intensity of the ambient light.

You can press ctrl-c to exit the script anytime.

The code:

Since we need the PWM, we initialize the mraa and PWM pin D5 (Step1, 2, 4, 5)

For initializing and using the light sensor, we use the “GroveLight” object from the upm namesapce (step 2).

upm::GroveLight *light = NULL;   /* Will hold pointer to light sensor object */

The PWM is initialized and enabled at pin D5:

pwmPin = mraa_pwm_init(LED_PWM_PIN);
mraa_pwm_period_us(pwmPin, 5000);      // Set the period as 5000 us or 5ms
mraa_pwm_enable(pwmPin, 1);

Then, in a while loop, we keep reading the light sensor value using the “value” method which returns the intensity in ‘lux’ units.

ambientLight = light->value();

Thus read value is normalized and used to control the PWM duty cycle and to control the bar displayed in the console (step 6,7,8,9).


C/C++ on Intel Edison/Galileo – part5:Temperature sensor

In this blog post, we will explore the ways of interfacing a temperature sensor using both mraa and upm.

The Grove temperature sensor that we are going to use is a class of transducer known as the thermistor. It is called so because it’s resistance (the res’istor’) part is controlled by the thermal energy (‘therm’al part) applied on the sensor. We are going to use the Grove temperature sensor.

Continue reading

C/C++, Edison, Intel Edison, Intel Galileo, IoT, IoT Linux image, Linux, mraa, Uncategorized, upm

C/C++ on Intel Edison/Galileo – part4:ADC

ADC is a peripheral that lets you input an analog signal and outputs the digital representation of the input analog signal.
The world in which we live in is surrounded by the analog signals. The temperature, sound that hear, the light that we see are all analog signals. If you want to interact or measure these signal in a digital system like Galileo/Edison, you’ll have to use ADC a.k.a Analog to Digital Converter.

Continue reading

C/C++, Edison, Intel Edison, Intel Galileo, IoT, IoT Linux image, Linux, mraa, PWM, Tutorial

C/C++ on Intel Edison/Galileo – part3: PWM

In this blog post, we are going to look into the ways of mraa functions used for controlling the PWM module available on the Galileo/Edison. In this tutorial, we will using PWM to control the intensity with which the LED glows, connected to port D5. The LED starts of with lowest intensity and then gradually increase the intensity until it reaches the maximum intensity and then decreases the intensity gradually until the LED is at the lowest intensity and the cycle continues.

Continue reading

C/C++, Edison, GPIO, Intel Edison, Intel Galileo, IoT, Linux, mraa, Uncategorized

C/C++ on Intel Edison/Galileo – part2: Buttons

In this post, we will be looking at using mraa library on C for interfacing with a button. For this example, the button will be used to turn on and off an LED connected to the Edison/Galileo. This example is going to be very similar to the previous one. The only difference being that the state of the LED is controlled by a button instead of the program running on Edison/Galileo itself.

Continue reading

Intel Edison, Linkit smart 7688, Linkit smart 7688 Duo, Linux, Uncategorized

Intel Edison v/s Linkit smart 7688

Thanks to Seeed studio, I recently came in possession of their latest Linkit modules, Linkit smart 7688 and Linkit smart 7688 duo.

As soon as I saw these modules, the first question that came to my mind was how they hold up against Intel’s Edison. Since both of these modules are advertised as IoT modules with lot of features packed into a single tiny module the comparison was obvious.

Continue reading

C/C++, GPIO, Intel Edison, Intel Galileo, mraa, Tutorial, Uncategorized

C on Intel Edison/Galileo – part1

In this series of blog posts, I’ll be posting on writing ‘C’ code on Intel Edison/Galileo platform. I’ll be using the Grove kit. You can also use a bread board instead of Grove kit.

There are two libraries available on the Intel Edison/Galileo for developing applications written in C that need to use sensors, actuators, LEDs etc…

  • MRAA: Provides API for interfacing with the GPIOs, ADCs, PWM, SPI, etc… It is basically for interfacing the low level peripherals. It is kind of bare bones, you can use the functions provided by MRAA to drive more complicated peripherals like sensors or you can use…
  • UPM: Provides higher levels of abstractions via objects for controlling things like LCDs, temperature sensors etc… It is a level above the MRAA and most of the functions that you might need while interfacing a sensor or LCD are already implemented in UPM.

Continue reading