The code is developed for the hardware NUCLEO-L432KC or any other board with SPI communication and the Digilent Pmod ALS1 ambient light sensor module. The purpose is to make students to understand the development process. They need to study the original analog to digital converter documentation to understand how the converter 16 bit output is converted to an illuminance value.

Committer:
timo_k2
Date:
Tue Nov 17 16:01:26 2020 +0000
Revision:
0:fb0f74ee7b70
Child:
1:444f907aa5eb
The initial commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
timo_k2 0:fb0f74ee7b70 1 /* mbed Microcontroller Library
timo_k2 0:fb0f74ee7b70 2 * Copyright (c) 2019 ARM Limited
timo_k2 0:fb0f74ee7b70 3 * SPDX-License-Identifier: Apache-2.0
timo_k2 0:fb0f74ee7b70 4 ************************************************************************
timo_k2 0:fb0f74ee7b70 5 *
timo_k2 0:fb0f74ee7b70 6 * Test of the Pmod ambient light sensor
timo_k2 0:fb0f74ee7b70 7 *
timo_k2 0:fb0f74ee7b70 8 *************************************************************************
timo_k2 0:fb0f74ee7b70 9 * Description: McLab08_SPI_Pmod_ALS1_OS6
timo_k2 0:fb0f74ee7b70 10 * The ambient light value will be read and converted to LUX.
timo_k2 0:fb0f74ee7b70 11 *
timo_k2 0:fb0f74ee7b70 12 * Material
timo_k2 0:fb0f74ee7b70 13 * 1. ST NUCLEO L432KC , NXP FRDM-K64F
timo_k2 0:fb0f74ee7b70 14 * or some other micro controller board with SPI communication
timo_k2 0:fb0f74ee7b70 15 * 2. Digilent Pmod ALS ambient light sensor
timo_k2 0:fb0f74ee7b70 16 * Please connect L432KC or FRDM-K64F - PmodALS with lines:
timo_k2 0:fb0f74ee7b70 17 * L432KC D13 - ALS 4 SCK hardware defined for the SPI
timo_k2 0:fb0f74ee7b70 18 * L432KC D12 - ALS 3 MISO hardware defined for the SPI
timo_k2 0:fb0f74ee7b70 19 * L432KC D4 - ALS 1 CS or any other free
timo_k2 0:fb0f74ee7b70 20 * GND - ALS 5 GND
timo_k2 0:fb0f74ee7b70 21 * Vcc - ALS 6 Vcc
timo_k2 0:fb0f74ee7b70 22
timo_k2 0:fb0f74ee7b70 23 * ALS data on the SPI
timo_k2 0:fb0f74ee7b70 24 * D15 ... D12 - four zeros
timo_k2 0:fb0f74ee7b70 25 * D11 ... D04 - 8 bits of ambient light data
timo_k2 0:fb0f74ee7b70 26 * D03 ... D00 - four zeros
timo_k2 0:fb0f74ee7b70 27 *
timo_k2 0:fb0f74ee7b70 28 * Details on the ADC IC on ALS board are given at
timo_k2 0:fb0f74ee7b70 29 * http://www.ti.com/lit/ds/symlink/adc081s021.pdf
timo_k2 0:fb0f74ee7b70 30 * The Pmod ALS
timo_k2 0:fb0f74ee7b70 31 * https://reference.digilentinc.com/reference/pmod/pmodals/start
timo_k2 0:fb0f74ee7b70 32 *
timo_k2 0:fb0f74ee7b70 33 * Timo Karppinen 17.11.2020
timo_k2 0:fb0f74ee7b70 34 **************************************************************/
timo_k2 0:fb0f74ee7b70 35 #include "mbed.h"
timo_k2 0:fb0f74ee7b70 36
timo_k2 0:fb0f74ee7b70 37 DigitalOut LED(D1); // LD1 to LD4 pin names are linked to D13 in L432KC.
timo_k2 0:fb0f74ee7b70 38 // The board LD3 is the D13. Do not use. It is the SPISCK.
timo_k2 0:fb0f74ee7b70 39 // PmodALS
timo_k2 0:fb0f74ee7b70 40 SPI spi(D11, D12, D13); // mosi, miso, sck
timo_k2 0:fb0f74ee7b70 41
timo_k2 0:fb0f74ee7b70 42 DigitalOut alsCS(D4); // chip select for sensor SPI communication
timo_k2 0:fb0f74ee7b70 43
timo_k2 0:fb0f74ee7b70 44 int alsScaledI = 0; // 32 bit integer
timo_k2 0:fb0f74ee7b70 45 int getALS();
timo_k2 0:fb0f74ee7b70 46
timo_k2 0:fb0f74ee7b70 47 int main()
timo_k2 0:fb0f74ee7b70 48 {
timo_k2 0:fb0f74ee7b70 49 // SPI for the ALS
timo_k2 0:fb0f74ee7b70 50 // Setup the spi for 8 bit data, high steady state clock,
timo_k2 0:fb0f74ee7b70 51 // second edge capture, with a 12MHz clock rate
timo_k2 0:fb0f74ee7b70 52 spi.format(8,0);
timo_k2 0:fb0f74ee7b70 53 spi.frequency(12000000);
timo_k2 0:fb0f74ee7b70 54 // ready to wait the conversion start
timo_k2 0:fb0f74ee7b70 55 alsCS.write(1);
timo_k2 0:fb0f74ee7b70 56
timo_k2 0:fb0f74ee7b70 57 while (true) {
timo_k2 0:fb0f74ee7b70 58
timo_k2 0:fb0f74ee7b70 59 alsScaledI = getALS();
timo_k2 0:fb0f74ee7b70 60 printf("Ambient light scaled to LUX = '%0d' \r\n",alsScaledI);
timo_k2 0:fb0f74ee7b70 61
timo_k2 0:fb0f74ee7b70 62 if (alsScaledI > 100){
timo_k2 0:fb0f74ee7b70 63 LED.write(0);
timo_k2 0:fb0f74ee7b70 64 printf("Be aware of high UV radiation! \n\n");
timo_k2 0:fb0f74ee7b70 65 }
timo_k2 0:fb0f74ee7b70 66 else{
timo_k2 0:fb0f74ee7b70 67 LED.write(1);
timo_k2 0:fb0f74ee7b70 68 printf("Too low light for working \n\n");
timo_k2 0:fb0f74ee7b70 69 }
timo_k2 0:fb0f74ee7b70 70
timo_k2 0:fb0f74ee7b70 71 ThisThread::sleep_for(3000ms);
timo_k2 0:fb0f74ee7b70 72 }
timo_k2 0:fb0f74ee7b70 73 }
timo_k2 0:fb0f74ee7b70 74
timo_k2 0:fb0f74ee7b70 75 int getALS()
timo_k2 0:fb0f74ee7b70 76 {
timo_k2 0:fb0f74ee7b70 77 char alsByte0 = 0; //8bit data from sensor board, char is the unsigned 8bit
timo_k2 0:fb0f74ee7b70 78 char alsByte1 = 0; // 8bit data from sensor board
timo_k2 0:fb0f74ee7b70 79 char alsByteSh0 = 0;
timo_k2 0:fb0f74ee7b70 80 char alsByteSh1 = 0;
timo_k2 0:fb0f74ee7b70 81 char als8bit = 0;
timo_k2 0:fb0f74ee7b70 82 unsigned short alsRaw = 0; // unsigned 16 bit
timo_k2 0:fb0f74ee7b70 83 float alsScaledF = 0; // 32 bit floating point
timo_k2 0:fb0f74ee7b70 84
timo_k2 0:fb0f74ee7b70 85 // Begin the conversion process and serial data output
timo_k2 0:fb0f74ee7b70 86 alsCS.write(0);
timo_k2 0:fb0f74ee7b70 87 // Reading two 8bit bytes by writing two dymmy 8bit bytes
timo_k2 0:fb0f74ee7b70 88 alsByte0 = spi.write(0x00);
timo_k2 0:fb0f74ee7b70 89 alsByte1 = spi.write(0x00);
timo_k2 0:fb0f74ee7b70 90 // End of serial data output and back to tracking mode
timo_k2 0:fb0f74ee7b70 91 alsCS.write(1);
timo_k2 0:fb0f74ee7b70 92 // Check the http://www.ti.com/lit/ds/symlink/adc081s021.pdf
timo_k2 0:fb0f74ee7b70 93 // shifting bits to get the number out
timo_k2 0:fb0f74ee7b70 94 alsByteSh0 = alsByte0 << 4;
timo_k2 0:fb0f74ee7b70 95 alsByteSh1 = alsByte1 >> 4;
timo_k2 0:fb0f74ee7b70 96
timo_k2 0:fb0f74ee7b70 97 als8bit =( alsByteSh0 | alsByteSh1 );
timo_k2 0:fb0f74ee7b70 98
timo_k2 0:fb0f74ee7b70 99 alsRaw = als8bit;
timo_k2 0:fb0f74ee7b70 100 alsScaledF = (float(alsRaw))*(float(6.68));
timo_k2 0:fb0f74ee7b70 101 // The value 6.68 is 64 bit double precision floating point of type double.
timo_k2 0:fb0f74ee7b70 102 // Conversions to 32 bit floating point of type float.
timo_k2 0:fb0f74ee7b70 103
timo_k2 0:fb0f74ee7b70 104 printf("Ambient light raw 8 bit 0...255 = '%d' \r\n",alsRaw);
timo_k2 0:fb0f74ee7b70 105 //printf("Ambient light scaled to LUX = '%0.1f' \r\n",alsScaledF);
timo_k2 0:fb0f74ee7b70 106 //Sorry, no more float printing in OS6 !
timo_k2 0:fb0f74ee7b70 107 return (int)alsScaledF;
timo_k2 0:fb0f74ee7b70 108 }
timo_k2 0:fb0f74ee7b70 109