The code is developed for the hardware NUCLEO-L432KC or any other board with analog input with 12 bit resolution. This version of code works with a photo diode and operational amplifier. The amplifier circuit feedback resistor value and capasitor value define the gain and the time constant. The purpose is to make students to understand the development process.

Committer:
timo_k2
Date:
Tue Sep 14 11:39:19 2021 +0000
Revision:
0:36df8e1d4d07
Initial commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timo_k2 0:36df8e1d4d07 1 /* mbed Microcontroller Library
timo_k2 0:36df8e1d4d07 2 * Copyright (c) 2019 ARM Limited
timo_k2 0:36df8e1d4d07 3 * SPDX-License-Identifier: Apache-2.0
timo_k2 0:36df8e1d4d07 4 ************************************************************************
timo_k2 0:36df8e1d4d07 5 *
timo_k2 0:36df8e1d4d07 6 * Test of the Pmod ambient light sensor, code for task 1
timo_k2 0:36df8e1d4d07 7 *
timo_k2 0:36df8e1d4d07 8 *************************************************************************
timo_k2 0:36df8e1d4d07 9 * Description: McLab13_Sensor_SPI_PmodALS_OS6_task1
timo_k2 0:36df8e1d4d07 10 * The ambient light value will be read and converted to LUX.
timo_k2 0:36df8e1d4d07 11 *
timo_k2 0:36df8e1d4d07 12 * Material
timo_k2 0:36df8e1d4d07 13 * 1. ST NUCLEO L432KC , NXP FRDM-K64F
timo_k2 0:36df8e1d4d07 14 * or some other micro controller board
timo_k2 0:36df8e1d4d07 15 *
timo_k2 0:36df8e1d4d07 16 * 2. Option for task1: Photo Diode BPW34, OpAmp TLC721, 330k, 12 pF
timo_k2 0:36df8e1d4d07 17 * Detailed circuit diagram in the instruction sheet.
timo_k2 0:36df8e1d4d07 18 *
timo_k2 0:36df8e1d4d07 19 *
timo_k2 0:36df8e1d4d07 20 * Timo Karppinen 25.11.2020
timo_k2 0:36df8e1d4d07 21 **************************************************************/
timo_k2 0:36df8e1d4d07 22 #include "mbed.h"
timo_k2 0:36df8e1d4d07 23
timo_k2 0:36df8e1d4d07 24 DigitalOut LED(D2); // A pin for connecting a LED
timo_k2 0:36df8e1d4d07 25
timo_k2 0:36df8e1d4d07 26 AnalogIn ainPD(A0); // A pin to interface OpAmp output 0V to 3,3V
timo_k2 0:36df8e1d4d07 27
timo_k2 0:36df8e1d4d07 28 int alScaled = 0; // The voltage converted to illuminance Lux value
timo_k2 0:36df8e1d4d07 29
timo_k2 0:36df8e1d4d07 30 unsigned short pd12bit = 0; // 16 bit variable to store the conversion result
timo_k2 0:36df8e1d4d07 31 // from the internal analog to digital converter
timo_k2 0:36df8e1d4d07 32 // in the L432KC the ADC resolution is 12 bit.
timo_k2 0:36df8e1d4d07 33
timo_k2 0:36df8e1d4d07 34 float pdScaledF = 0.0; // for the value calculated for the correct scale
timo_k2 0:36df8e1d4d07 35
timo_k2 0:36df8e1d4d07 36 int main()
timo_k2 0:36df8e1d4d07 37 {
timo_k2 0:36df8e1d4d07 38
timo_k2 0:36df8e1d4d07 39 while (true) {
timo_k2 0:36df8e1d4d07 40
timo_k2 0:36df8e1d4d07 41 pd12bit = ainPD.read_u16() >>4; // leftmost 12 bits moved 4 bits to right.
timo_k2 0:36df8e1d4d07 42 printf("Ambient light PD sensor 12 bit 0...4095 = '%d' \r\n", pd12bit);
timo_k2 0:36df8e1d4d07 43 pdScaledF = (float(pd12bit))*(float(0.1));// write the correct scaling multiplier
timo_k2 0:36df8e1d4d07 44 if (pdScaledF > 100){
timo_k2 0:36df8e1d4d07 45 LED.write(0);
timo_k2 0:36df8e1d4d07 46 printf("Light OK for working. \n\n");
timo_k2 0:36df8e1d4d07 47 }
timo_k2 0:36df8e1d4d07 48 else{
timo_k2 0:36df8e1d4d07 49 LED.write(1);
timo_k2 0:36df8e1d4d07 50 printf("Too low light for working. \n\n");
timo_k2 0:36df8e1d4d07 51 }
timo_k2 0:36df8e1d4d07 52
timo_k2 0:36df8e1d4d07 53 ThisThread::sleep_for(3000ms);
timo_k2 0:36df8e1d4d07 54 }
timo_k2 0:36df8e1d4d07 55 }
timo_k2 0:36df8e1d4d07 56