You are on page 1of 106

TP de l’informatique industrielle

Activity 1: Temperature Sensor Module (KY-001):


Description:
The DS18B20 digital thermometer provides 9-bit to 12-bit Celsius temperature measurements
and has an alarm function with non-volatile, user-programmable high and low trigger points,
meaning that the sensor has programmable high and low limits that cannot change on their own.
The DS18B20 communicates over a 1-wire bus, which by definition requires only one data line
(and ground) to communicate with a central microprocessor. In addition, the DS18B20 can
draw power directly from the data line ("parasite power"). This eliminates the need for an
external power source. Each DS18B20 has a unique 64-bit serial code, allowing multiple
DS18B20s to operate on the same 1-Wire bus. This then opens up the possibility of using only
one microprocessor to control/evaluate multiple DS18B20s distributed over a wide area.
Applications that can benefit from this feature include HVAC environmental controls,
temperature monitoring systems in buildings, plants or machinery, and process monitoring and
control systems.

PIN Assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage:

Code :
// Required libraries will be imported
#include <OneWire.h>
#include <DallasTemperature.h>

// Here the input pin is declared to which the sensor module is


connected
#define KY001_Signal_PIN 4

// Libraries are configured


OneWire oneWire(KY001_Signal_PIN);
DallasTemperature sensors(&oneWire);

void setup() {

// Initialize serial output


Serial.begin(9600);
Serial.println("KY-001 temperature measurement");

// Sensor is initialized
sensors.begin();
}

//main program loop


void loop()
{
// Temperature measurement is started...
sensors.requestTemperatures();

Pr Jarou Tarik
TP de l’informatique industrielle

// ... and output measured temperature


Serial.print("Temperature: ");
Serial.print(sensors.getTempCByIndex(0));
Serial.write(176); // UniCode specification of a char symbol for
the "°" symbol
Serial.println("C");

delay(1000); // 5s pause until next measurement


}

Activity 2: Vibration Switch Module (KY-002):


Description:
This sensor is a module that can sense vibrations and emits a signal at each vibration. It consists
of a simple conductive outer shell, which closes contact with the internal spring in case of
vibration and thus emits a signal.

PIN Assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage:

Code:
int Led = 13 ;// declaration of the LED output pin
int Sensor = 10 ;// Declaration of the sensor input pin
int val; // Temporary variable

void setup ()
{
pinMode (Led, OUTPUT) ; // Initialize output pin
pinMode (Sensor, INPUT) ; // Initialize sensor pin
digitalWrite(Sensor, HIGH) ; // Activate internal pull-up resistor
}

void loop ()
{
val = digitalRead (Sensor) ; // The current signal at the sensor
is read out

if (val == HIGH) // If a signal could be detected, the LED is


switched on.

Pr Jarou Tarik
TP de l’informatique industrielle

{
digitalWrite (Led, LOW);
}
else
{
digitalWrite (Led, HIGH);
}
}

Activity 3: Hall Magnetic-Field Sensor Module (KY-003):


Description:
Hall-effect switches are monolithic integrated circuits with tighter magnetic specifications,
meaning that the sensor is made of one piece, has increased sensitivity to magnetic fields, and
that everything needed is already built directly into the sensor itself, which is suitable for
continuous operation over temperatures up to +150°C and is more stable to temperature as well
as supply voltage changes. Each unit includes a voltage regulator for operation with supply
voltages from 4.5 to 24 volts, a reverse polarity protection diode, a square Hall voltage
generator, temperature compensation circuitry, small signal amplifier, Schmitt trigger and an
open collector output to sink up to 25 mA. The transistor switches through if the module is held
in a magnetic field. This can then be read out at the signal output as an analog voltage value.

Pr Jarou Tarik
TP de l’informatique industrielle

PIN Assignment:

Montage:

Code:
int Led = 13 ;// declaration of the LED output pin
int Sensor = 10 ;// Declaration of the sensor input pin
int val; // Temporary variable

void setup ()
{
pinMode (Led, OUTPUT) ; // Initialize output pin
pinMode (Sensor, INPUT) ; // Initialize sensor pin

Pr Jarou Tarik
TP de l’informatique industrielle

digitalWrite(Sensor, HIGH) ; // Activate internal pull-up resistor


}

void loop ()
{
val = digitalRead (Sensor) ; // The current signal at the sensor
is read out

if (val == HIGH) // If a signal could be detected, the LED is


switched on.
{
digitalWrite (Led, LOW);
}
else
{
digitalWrite (Led, HIGH);
}
}

Activity 4: Button Module (KY-004):


Description:
When the button is pressed, two signal outputs are shorted together.

Pr Jarou Tarik
TP de l’informatique industrielle

PIN Assignment:

Montage:

Code:
int Led = 13 ;// declaration of the LED output pin
int Sensor = 10 ;// Declaration of the sensor input pin
int val; // Temporary variable

void setup ()
{

Pr Jarou Tarik
TP de l’informatique industrielle

pinMode (Led, OUTPUT) ; // Initialize output pin


pinMode (Sensor, INPUT) ; // Initialize sensor pin
digitalWrite(Sensor, HIGH) ; // Activate internal pull-up resistor
}

void loop ()
{
val = digitalRead (Sensor) ; // The current signal at the sensor
is read out

if (val == HIGH) // If a signal could be detected, the LED is


switched on.
{
digitalWrite (Led, LOW);
}
else
{
digitalWrite (Led, HIGH);
}
}

Activity 5: Infrared Transmitter Module (KY-005):


Description:
A light emitting diode that emits in the infrared range. Depending on the input voltage, series
resistors are required.

Pr Jarou Tarik
TP de l’informatique industrielle

PIN Assignment:

Montage:

Code:
int Led = 3;
void setup ()
{
pinMode (Led, OUTPUT); // Initialize output pin for the LED

Pr Jarou Tarik
TP de l’informatique industrielle

}
void loop () //Main program loop
{
digitalWrite (Led, HIGH); // LED is switched on
delay (4000); // Wait mode for 4 seconds
digitalWrite (Led, LOW); // LED is switched off
delay (2000); // Wait mode for another two seconds during which
the LED is switched off
}

Activity 6: Passive Piezo-Buzzer Module (KY-006):


Description:
Controlled with PWM signals of different frequencies, the passive piezo buzzer can be used to
generate different sounds.

PIN Assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage:

Code:
int buzzer = 8 ; // Declaration of the buzzer output pin

void setup ()
{
pinMode (buzzer, OUTPUT) ;// Initialize as output pin
}

void loop ()
{
unsigned char i;
while (1)
{
// In this program, the buzzer is controlled alternately with
two different frequencies.
// The signal consists of a square wave voltage.

Pr Jarou Tarik
TP de l’informatique industrielle

// Turning the buzzer on and off will generate a tone that


roughly corresponds to the frequency.
// The frequency is defined by the length of the on and off
phase.

//Frequency 1
for (i = 0; i <80; i++)
{
digitalWrite (buzzer, HIGH) ;
delay (1) ;
digitalWrite (buzzer, LOW) ;
delay (1) ;
}
//Frequency 2
for (i = 0; i <100; i++)
{
digitalWrite (buzzer, HIGH) ;
delay (2) ;
digitalWrite (buzzer, LOW) ;
delay (2) ;
}
}
}

Activity 7: RGB LED SMD Module (KY-009):


Description:
LED module which contains a red, blue and green LED. These are connected to each other by
means of a common cathode.

Pr Jarou Tarik
TP de l’informatique industrielle

PIN Assignment:

Montage:

Code:
int Led_Red = 10;
int Led_Green = 11;
int Led_Blue = 12;

void setup ()

Pr Jarou Tarik
TP de l’informatique industrielle

{
// Initialize output pins for the LEDs
pinMode (Led_Red, OUTPUT);
pinMode (Led_Green, OUTPUT);
pinMode (Led_Blue, OUTPUT);
}

void loop () //Main program loop


{
digitalWrite (Led_Red, HIGH); // LED is switched on
digitalWrite (Led_Green, LOW); // LED is switched on
digitalWrite (Led_Blue, LOW); // LED is switched on
delay (3000); // Wait mode for 3 seconds

digitalWrite (Led_Red, LOW); // LED is switched on


digitalWrite (Led_Green, HIGH); // LED is switched on
digitalWrite (Led_Blue, LOW); // LED is switched on
delay (3000); // Waiting mode for another three seconds in which
the LEDs are then switched over

digitalWrite (Led_Red, LOW); // LED is switched on


digitalWrite (Led_Green, LOW); // LED is switched on
digitalWrite (Led_Blue, HIGH); // LED is switched on
delay (3000); // Wait mode for another three seconds in which the
LEDs are then switched over
}

Pr Jarou Tarik
TP de l’informatique industrielle

Activity 8: Light-barrier-Module (KY-010):


Description:
When the light barrier of the module is interrupted, the signal coming from the module itself
is also interrupted.

PIN Assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage:

Code:
int Led = 13 ;// declaration of the LED output pin
int Sensor = 10 ;// Declaration of the sensor input pin
int val; // Temporary variable

void setup ()
{
pinMode (Led, OUTPUT) ; // Initialize output pin
pinMode (Sensor, INPUT) ; // Initialize sensor pin
digitalWrite(Sensor, HIGH) ; // Activate internal pull-up resistor
}

void loop ()
{
val = digitalRead (Sensor) ; // The current signal at the sensor
is read out

Pr Jarou Tarik
TP de l’informatique industrielle

if (val == HIGH) // If a signal could be detected, the LED is


switched on.
{
digitalWrite (Led, LOW);
}
else
{
digitalWrite (Led, HIGH);
}

Activity 9: 2-COLOR 5MM LED MODULE (KY-011):


Description:
LED module which contains a red and green LED. These are connected to each other by means
of a common cathode.
PIN assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage:

ARDUINO SENSOR
Pin 10 LED GREEN
Pin 11 LED RED
Ground GND
ARDUINO SENSOR
Pin 13 LED+
ground LED-

Code:
This code example shows how the integrated LEDs can be changed alternately, every 3 seconds,
by means of a definable output pin.
int Led_Red = 10;
int Led_Green = 11;

void setup ()
{
// Initialize output pins for the LEDs
pinMode (Led_Red, OUTPUT);
pinMode (Led_Green, OUTPUT);

Pr Jarou Tarik
TP de l’informatique industrielle

void loop () //Main program loop


{
digitalWrite (Led_Red, HIGH); // LED is switched on
digitalWrite (Led_Green, LOW); // LED is switched on
delay (3000); // Wait mode for 3 seconds

digitalWrite (Led_Red, LOW); // LED is switched on


digitalWrite (Led_Green, HIGH); // LED is switched on
delay (3000); // Wait mode for another two seconds in which the
LEDs are then switched on
}

Activity 10: ACTIVE PIEZO BUZZER MODULE (KY-012):


Description:
When the light barrier of the module is interrupted, the signal coming from the module itself is
also interrupted.
PIN assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage:

ARDUINO SENSOR
Pin 13 LED+
ground LED-
ARDUINO SENSOR
pin 10 signal
5V +V
ground GND

Code:
This is a sample program that lights up an LED when a signal is detected at the sensor. The
modules KY-011, KY-016 or KY-029 can also be used as LEDs, for example.
int Led = 13 ;// declaration of the LED output pin
int Sensor = 10 ;// Declaration of the sensor input pin
int val; // Temporary variable

void setup ()
{
pinMode (Led, OUTPUT) ; // Initialize output pin
pinMode (Sensor, INPUT) ; // Initialize sensor pin
digitalWrite(Sensor, HIGH) ; // Activate internal pull-up resistor

Pr Jarou Tarik
TP de l’informatique industrielle

void loop ()
{
val = digitalRead (Sensor) ; // The current signal at the sensor
is read out

if (val == HIGH) // If a signal could be detected, the LED is


switched on.
{
digitalWrite (Led, LOW);
}
else
{
digitalWrite (Led, HIGH);
}
}

Activity 11: TEMPERATURE SENSOR MODULE (KY-013):


Description:
This module contains a NTC thermistor which can measure temperatures in the range of -55°C
up to +125°C. This has a decreasing resistance value at higher temperature.
This change in resistance can be mathematically approximated and converted into a linear curve
and the temperature coefficient (dependence of resistance change on temperature change) can
be determined. Using these, the current temperature can then always be calculated if the current
resistance is known.
This resistance can be determined with the help of a voltage divider, where a known voltage is
divided over a known and an unknown (variable) resistance. Using this measured voltage, the
resistance can then be calculated - the exact calculation is included in the code examples below.

Pr Jarou Tarik
TP de l’informatique industrielle

PIN assignment:

Montage :

ARDUINO SENSOR
A0 (KY-053 ADC) Signal
5V +V
Masse GND

Pr Jarou Tarik
TP de l’informatique industrielle

ARDUINO KY-053
5V +V
Masse GND
A5 SCL
A4 SDA

Code :
For the following code example an additional library is needed:
Adafruit_ADS1x15 by Adafruit | published under the BSD License.
The example below uses this said library - for this we recommend downloading it from Github,
unzipping it and copying it in the Arduino library folder, which by default is located at
(C:\User[username]\Documents\Arduino\libraries), so that it is available for this code example
and following projects. Alternatively, this is also included in the download package below as
well.
The program measures the current voltage value at the NTC, calculates the temperature and
translates the result to °C for serial output

#include <Adafruit_ADS1015.h>
#include <math.h>

Adafruit_ADS1115 ads;

void setup(void)
{
Serial.begin(9600);

Serial.println("Values of analog input A1 of ADS1115 are read and


output");
Serial.println("ADC Range: +/- 4.096V 1 bit = 0.125mV");

// This module has signal amplifiers at its analog inputs, whose


// amplification can be configured via software in the ranges
below
// can be configured.

Pr Jarou Tarik
TP de l’informatique industrielle

// This is desired in case a certain voltage range is expected //


as measurement result and
// range is expected as a measurement result and thus a higher
resolution of the signal is
// is obtained.
// Gain=[2/3] is selected as default gain and can be changed by
commenting out // to another gain.
// to change to a different gain.
// ADS1115
// -------
// ads.setGain(GAIN_TWOTHIRDS); // 2/3x gain +/- 6.144V 1 bit =
0.1875mV
ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 0.125mV
// ads.setGain(GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 0.0625mV
// ads.setGain(GAIN_FOUR); // 4x gain +/- 1.024V 1 bit = 0.03125mV
// ads.setGain(GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit =
0.015625mV
// ads.setGain(GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit =
0.0078125mV

ads.begin();
}

void loop(void)

Activity 12: COMBI-SENSOR TEMPERATURE+HUMIDITY (KY-015):


Description:
This sensor is a mixture of temperature sensor and humidity sensor in a compact design -
however, the disadvantage is the low sampling rate of the measurement, so that only every 2
seconds a new measurement result is available - this sensor is therefore very well suited for
long-term measurements.

Pr Jarou Tarik
TP de l’informatique industrielle

PIN assignment:

Montage :

ARDUINO SENSOR
Pin D2 Signal

5V +V

Ground GND

Pr Jarou Tarik
TP de l’informatique industrielle

For the following code example an additional library is needed:


DHT-sensor-library by Adafruit | published under the MIT License.
This sensor does not output its measurement result as an analog signal to an output pin, but
communicates it digitally encoded.
The example below uses this said library - for this we recommend to download it from Github,
unzip it and copy it in the Arduino library folder which is located by default in
(C:\User[username]\Documents\Arduino\libraries) so that it is available for this code example
and following projects. Alternatively, this is also included in the download package below as
well.
Code:
// Adafruit_DHT library is inserted
#include "DHT.h"

// Here the respective input pin can be declared


#define DHTPIN 2

// The sensor is initialized


#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);

void setup()
{
Serial.begin(9600);
Serial.println("KY-015 test - temperature and humidity test:");

// Measurement is started
dht.begin();
}

// Main program loop


// The program starts the measurement and reads out the measured
values

Pr Jarou Tarik
TP de l’informatique industrielle

// There is a pause of 2 seconds between measurements,


// so that a new measurement can be acquired on the next run.
void loop() {

// Two seconds pause between measurements


delay(2000);

// Humidity is measured
float h = dht.readHumidity();
// temperature is measured
float t = dht.readTemperature();

// Checking if the measurements have passed without errors

Activity 13: RGB 5MM LED MODULE (KY-016):


Description:
LED module which contains a red, blue and green LED. These are connected to each other by
means of a common cathode.
PIN assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage :
ARDUINO SENSOR
Pin 10 LED Red

Pin 11 LED Green

Pin 12 LED Blue

Ground GND

Code:
This code example shows how the integrated LEDs can be changed alternately, in 3 second
cycles, by means of a definable output pin.
int Led_Red = 10;
int Led_Green = 11;
int Led_Blue = 12;

void setup ()
{
// Initialize output pins for the LEDs
pinMode (Led_Red, OUTPUT);

Pr Jarou Tarik
TP de l’informatique industrielle

pinMode (Led_Green, OUTPUT);


pinMode (Led_Blue, OUTPUT);
}

void loop () //Main program loop


{
digitalWrite (Led_Red, HIGH); // LED is switched on
digitalWrite (Led_Green, LOW); // LED is switched on
digitalWrite (Led_Blue, LOW); // LED is switched on
delay (3000); // Wait mode for 3 seconds

digitalWrite (Led_Red, LOW); // LED is switched on


digitalWrite (Led_Green, HIGH); // LED is switched on
digitalWrite (Led_Blue, LOW); // LED is switched on
delay (3000); // Waiting mode for another three seconds in which
the LEDs are then switched over

digitalWrite (Led_Red, LOW); // LED is switched on


digitalWrite (Led_Green, LOW); // LED is switched on
digitalWrite (Led_Blue, HIGH); // LED is switched on
delay (3000); // Wait mode for another three seconds in which the
LEDs are then switched over
}

Activity 14: TILT SWITCH MODULE (KY-017):


Description:
Depending on the inclination, a switch closes the input pins briefly. This happens due to the
fact that the ball inside rolls back and forth and with the right inclination then finally closes the
contacts inside the housing.

Pr Jarou Tarik
TP de l’informatique industrielle

PIN assignment:

Montage :

ARDUINO SENSOR
pin 10 signal

5V +V

ground GND

ARDUINO SENSOR
Pin 13 LED +

ground LED -

Pr Jarou Tarik
TP de l’informatique industrielle

Code :
This is a sample program that lights up an LED when a signal is detected at the sensor. The
modules KY-011, KY-016 or KY-029 can also be used as LEDs, for example.
int Led = 13 ;// declaration of the LED output pin
int Sensor = 10 ;// Declaration of the sensor input pin
int val; // Temporary variable

void setup ()
{
pinMode (Led, OUTPUT) ; // Initialize output pin
pinMode (Sensor, INPUT) ; // Initialize sensor pin
}

void loop ()
{
val = digitalRead (Sensor) ; // The current signal at the sensor
is read out

if (val == HIGH) // If a signal could be detected, the LED is


switched on.
{
digitalWrite (Led, LOW);
}
else
{
digitalWrite (Led, HIGH);
}
}

Pr Jarou Tarik
TP de l’informatique industrielle

Activity 15: PHOTORESISTOR MODULE (KY-018):


Description:
Contains an LDR resistor whose resistance value decreases with brighter environment. This
resistance can be determined with the help of a voltage divider, where a known voltage is
divided over a known (10KΩ) and an unknown (variable) resistance. Using this measured
voltage, the resistance can then be calculated - the exact calculation is included in the code
examples below.
PIN assignment:

Montage :
ARDUINO SENSOR
pin A5 signal

5V +V

ground GND

Pr Jarou Tarik
TP de l’informatique industrielle

Code :
The program measures the current voltage value at the sensor, calculates the current resistance
value of the sensor from this and the known series resistance and outputs the results on the serial
output.
int sensorPin = A5; // Declare the input pin here

// Serial output in 9600 baud


void setup()
{
Serial.begin(9600);
}

// The program measures the current voltage value at the sensor,


// calculates from this and the known series resistance the current
// resistance value of the sensor and outputs the results to the
serial output

void loop()

Pr Jarou Tarik
TP de l’informatique industrielle

{
// Current voltage value is measured...
int rawValue = analogRead(sensorPin);
float voltage = rawValue * (5.0/1023) * 1000;

float resitance = 10000 * ( voltage / ( 5000.0 - voltage) );

// ... and here output to the serial interface


Serial.print("Voltage value:"); Serial.print(voltage);
Serial.print("mV");
Serial.print(", resistance value:"); Serial.print(resitance);
Serial.println("Ohm");
Serial.println("---------------------------------------");

delay(500);
}

Activity 16: 5V RELAIS MODULE (KY-019):


Description:
This module is a 5V relay for switching higher currents. The relay switches the higher voltage
through when 5V are applied to the voltage input of the switch.
The output header of the relay has two output terminals:
• The one marked "NC" for "normally closed" in the image below simply means that this
passage is shorted by default without electrical switching at the relay.
• That which is marked "NO" for "normally open" in the image below simply means that
this passage is open or disconnected by default without electrical switching at the relay.
PIN assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage :
ARDUINO SENSOR
pin 10 signal

5V +V

ground GND

Code:
The program simulates a blinker - it switches the relay in predefined time (delayTime) between
the two states (or output terminals).
int relay = 10; // Declares the pin to which the relay is connected

int delayTime = 1; // Value in seconds how long to wait between the


switchovers

void setup ()
{
pinMode (relay, OUTPUT); // The pin is declared as output
}

Pr Jarou Tarik
TP de l’informatique industrielle

// The program simulates a blinker - it switches the relay in


predefined
// time (delayTime) between the two states (or output terminals).
void loop ()
{
digitalWrite (relay, HIGH); // "NO" is now shorted;
delay (delayTime * 1000);
digitalWrite (relay, LOW); // "NC" is now shorted;
delay (delayTime * 1000);
}

Activity 17: Tilt-Switch-Module (KY-020):


Description:
Depending on the tilt, a switch short-circuits the input pins. This happens due to
the fact that the ball inside rolls back and forth and with the right tilt then short-
circuits the contacts inside the housing.

PIN assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage:

This is an example program which lights up a LED when a signal is detected at the sensor. The
modules KY-011, KY-016 or KY-029 can also be used as LEDs, for example.
Code:
int Led = 13 ;// declaration of the LED output pin
int Sensor = 10 ;// Declaration of the sensor input pin
int val; // Temporary variable

void setup ()
{
pinMode (Led, OUTPUT) ; // Initialize output pin
pinMode (Sensor, INPUT) ; // Initialize sensor pin
}

void loop ()
{
val = digitalRead (Sensor) ; // The current signal at the sensor
is read out

if (val == HIGH) // If a signal could be detected, the LED is


switched on.

Pr Jarou Tarik
TP de l’informatique industrielle

{
digitalWrite (Led, LOW);
}
else
{
digitalWrite (Led, HIGH);
}
}

Activity 18: MINI MAGNETIC REED MODULE (KY-021):


Description:
If a magnetic field is detected, the two input pins are short-circuited by pulling
them towards each other.

PIN assignment:

Montage :

Pr Jarou Tarik
TP de l’informatique industrielle

This is an example program which lights up a LED when a signal is detected at the sensor. The
modules KY-011, KY-016 or KY-029 can also be used as LEDs, for example.
Code:
int Led = 13 ;// declaration of the LED output pin
int Sensor = 10 ;// Declaration of the sensor input pin
int val; // Temporary variable

void setup ()
{
pinMode (Led, OUTPUT) ; // Initialize output pin
pinMode (Sensor, INPUT) ; // Initialize sensor pin
}

void loop ()
{
val = digitalRead (Sensor) ; // The current signal at the sensor
is read out

if (val == HIGH) // If a signal could be detected, the LED is


switched on.
{
digitalWrite (Led, LOW);
}
else
{

Pr Jarou Tarik
TP de l’informatique industrielle

digitalWrite (Led, HIGH);


}
}

Activity 19: INFRARED RECEIVER MODULE (KY-022):


Description:
This module can receive infrared signals and outputs them at the signal output as
a digital sequence.
In addition, the LED integrated on the module flashes briefly when an infrared
signal is detected.
Pin assignment:

Montage :

Pr Jarou Tarik
TP de l’informatique industrielle

Code:
/*
* SimpleReceiver.cpp
*
* Demonstriert den Empfang von NEC-IR-Codes mit IRrecv
*
* Copyright (C) 2020-2021 Armin Joachimsmeyer
* armin.joachimsmeyer@gmail.com
*
* Diese Datei ist Teil von Arduino-IRremote
https://github.com/Arduino-IRremote/Arduino-IRremote.
*
* MIT-Lizenz
*/

/*
* Geben Sie an, welche(s) Protokoll(e) für die Dekodierung
verwendet werden soll(en).
* * Wenn kein Protokoll definiert ist, sind alle Protokolle aktiv.
*/
//#define DECODE_DENON // Enthält Sharp
//#define DECODE_JVC
//#define DECODE_KASEIKYO
//#define DECODE_PANASONIC // dasselbe wie DECODE_KASEIKYO
//#define DECODE_LG
#define DECODE_NEC // Beinhaltet Apple und Onkyo
//#define DECODE_SAMSUNG

Pr Jarou Tarik
TP de l’informatique industrielle

//#define DECODE_SONY
//#define DECODE_RC5
//#define DECODE_RC6

//#define DECODE_BOSEWAVE
//#define DECODE_LEGO_PF
//#define DECODE_MAGIQUEST
//#define DECODE_WHYNTER

//#define DECODE_DISTANCE // Universaldecoder für Impulsbreiten-


oder Impulsabstandsprotokolle
//#define DECODE_HASH // Spezialdecoder für alle Protokolle

#include <Arduino.h>

/*
* Makros für Eingangs- und Ausgangspin definieren etc.
*/
#include "PinDefinitionsAndMore.h"

#include <IRremote.h>

void setup() {
Serial.begin(115200);
// Nur um zu wissen, welches Programm auf meinem Arduino läuft
Serial.println(F("START " __FILE__ " von " __DATE__ "\r\nUsing
library version " VERSION_IRREMOTE));

/*
* Starten des Empfängers, Aktivieren der Feedback-LED und
Abgreifen des LED-Feedback-Pins von der internen Kartendefinition

Pr Jarou Tarik
TP de l’informatique industrielle

*/
IrReceiver.begin(IR_RECEIVE_PIN, ENABLE_LED_FEEDBACK,
USE_DEFAULT_FEEDBACK_LED_PIN);

Serial.print(F("Bereit zum Empfang von IR-Signalen an Pin "));


Serial.println(IR_RECEIVE_PIN);
}

void loop() {
/*
* Prüfen, ob empfangene Daten vorhanden sind und wenn ja,
versuchen, diese zu dekodieren.
* Dekodiertes Ergebnis steht in der Struktur
IrReceiver.decodedIRData.
*
* Z.B. Befehl ist in IrReceiver.decodedIRData.command
* Adresse ist in Befehl ist in IrReceiver.decodedIRData.address
* und bis zu 32 Bit Rohdaten in
IrReceiver.decodedIRData.decodedRawData
*/
if (IrReceiver.decode()) {

// Drucken einer kurzen Zusammenfassung der empfangenen


Daten
IrReceiver.printIRResultShort(&Serial);
if (IrReceiver.decodedIRData.protocol == UNKNOWN) {
// Wir haben hier ein unbekanntes Protokoll, drucken Sie
weitere Informationen
IrReceiver.printIRResultRawFormatted(&Serial, true);
}
Serial.println();

Pr Jarou Tarik
TP de l’informatique industrielle

/*
* !!!Wichtig!!! Aktivieren Sie den Empfang des nächsten
Wertes,
* da der Empfang nach dem Ende des aktuell empfangenen
Datenpakets gestoppt wurde.
*/
IrReceiver.resume(); // Empfang des nächsten Wertes
freigeben

/*
* Abschließend prüfen Sie die empfangenen Daten und führen
Aktionen entsprechend dem empfangenen Befehl aus
*/
if (IrReceiver.decodedIRData.command == 0x34) {
Serial.println("Signal empfangen");
} else if (IrReceiver.decodedIRData.command == 0x36) {
Serial.println("Signal empfangen und dieses Mal ist es
ein anderes");
} else {
Serial.println("Signal empfangen, aber leider nicht das
richtige");
}
}
}

Activity 20: JOYSTICK MODULE (XY-AXIS) (KY-023):


Description:
X and Y position of the joystick, are output as analog voltage on the output pins.
In this joystick, a separate potentiometer was installed for the X-axis, as well as for
the Y-axis. These result in a voltage divider, like this one which is shown in the
following picture.

Pr Jarou Tarik
TP de l’informatique industrielle

In the idle state, the potentiometer is in the middle, so that resistor1=resistor2, with which also
the applied voltage distributes itself equally on both - e.g. measured value At +V=5V -> 2.5V.
If now the position of the e.g. X-axis is changed, the respective resistors change depending on
the current position - e.g. if it goes in one direction, resistor 1 becomes smaller and resistor 2
larger, if it goes in the other direction, resistor 1 becomes larger and resistor 2 smaller.
Depending on how the resistors are distributed among each other, this results in a respective
voltage value that can be measured between the resistors (in the case of the potentiometer, so-
called sliders) and thus the position of the axis can be determined.

PIN assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage :

The program reads the current values of the input pins, converts them to a voltage (0-1023 ->
0V-5V) and outputs it on the serial output.
Code:
// Declaration and initialization of the input pins
int JoyStick_X = A0; // X-axis signal
int JoyStick_Y = A1; // Y-axis signal
int Button = 3; // Button

void setup ()
{
pinMode (JoyStick_X, INPUT);
pinMode (JoyStick_Y, INPUT);
pinMode (Button, INPUT);

// Since the button pulls the signal to ground when pressed,

Pr Jarou Tarik
TP de l’informatique industrielle

// here we switch on the pullup resistor


digitalWrite(Button, HIGH);

Serial.begin (9600); // Serial output with 9600 bps


}

// The program reads the current values of the input pins


// and outputs them to the serial output
void loop ()
{
float x, y;
int button;

//Actual values are read, converted to the voltage value....


x = analogRead (JoyStick_X) * (5.0 / 1023.0);
y = analogRead (JoyStick_Y) * (5.0 / 1023.0);
Button = digitalRead (Button);

//... and output at this position


Serial.print ("X axis:"); Serial.print (x, 4); Serial.print ("V,
");
Serial.print ("Y axis:"); Serial.print (y, 4); Serial.print ("V,
");
Serial.print ("Button:");

if(button==1)
{
Serial.println (" not pressed");
}
else
{

Pr Jarou Tarik
TP de l’informatique industrielle

Serial.println (" pressed");


}
delay (200);
}

Activity 21: LINEAR MAGNETIC HALL SENSOR (KY-024):


Description:
Hall-effect switches are monolithic integrated circuits with tighter magnetic specifications
(meaning that the sensor is one piece, has increased sensitivity to magnetic fields, and that
everything needed is already built directly into the sensor), suitable for continuous operation
over temperatures up to +150°C, and are more stable to temperature and supply voltage
changes. Each unit includes a voltage regulator for operation with supply voltages from 4.5 to
24 volts, a reverse polarity protection diode, a square Hall voltage
generator, temperature compensation circuitry, small signal amplifier,
Schmitt trigger and an open collector output to sink up to 25 mA. The
transistor switches through if the module is held in a magnetic field. This
can then be read out at the signal output as an analog voltage value. The
sensitivity of the sensor can be controlled by means of the potentiometer.

PIN assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage :

The program reads the current voltage value which can be measured at the analog output and
outputs it to the serial interface.
In addition, the state of the digital pin is also indicated in the console, which means whether the
value has fallen below the limit or not.
Code:
// Declaration and initialization of the input pins
int Analog_Input = A0; // Analog output of the sensor
int Digital_Input = 3; // Digital output of the sensor

void setup ()
{
pinMode (Analog_Input, INPUT);
pinMode (Digital_Input, INPUT);

Serial.begin (9600); // Serial output with 9600 bps

Pr Jarou Tarik
TP de l’informatique industrielle

// The program reads the current values of the input pins


// and outputs them to the serial output
void loop ()
{
float Analog;
int Digital;

//Actual values are read, converted to the voltage value....


Analog = analogRead (Analog_Input) * (5.0 / 1023.0);
Digital = digitalRead (Digital_Input);

//... and output at this position


Serial.print ("Analog voltage value:"); Serial.print (Analog, 4);
Serial.print ("V, ");
Serial.print ("Limit value:");

if(Digital==1)
{
Serial.println (" reached");
}
else
{
Serial.println (" not yet reached");
}
Serial.println ("-------------------------------------------------
---------------");
delay (200);
}

Pr Jarou Tarik
TP de l’informatique industrielle

Activity 22: REED MODULE (KY-025):


Description:
If a magnetic field is detected, this is output at the digital output. Reed contacts
have the property that their two thin contact springs, located inside the glass tube,
move towards each other if a magnetic field is nearby.
Digital output: If a magnetic field is detected, a signal is output here.
Analog output: Direct measured value of the sensor unit.
LED1: Indicates that the sensor is supplied with voltage.
LED2: Indicates that a magnetic field has been detected.
PIN assignment:

Montage :

Pr Jarou Tarik
TP de l’informatique industrielle

The program reads the current voltage value which can be measured at the analog output and
outputs it to the serial interface.
In addition, the state of the digital pin is also indicated in the console, which means whether the
value has fallen below the limit or not.

Code:
// Declaration and initialization of the input pins
int Analog_Input = A0; // Analog output of the sensor
int Digital_Input = 3; // Digital output of the sensor

void setup ()
{
pinMode (Analog_Input, INPUT);
pinMode (Digital_Input, INPUT);

Serial.begin (9600); // Serial output with 9600 bps


}

// The program reads the current values of the input pins


// and outputs them on the serial output
void loop ()
{
float Analog;
int Digital;

//Actual values are read, converted to the voltage value....


Analog = analogRead (Analog_Input) * (5.0 / 1023.0);

Pr Jarou Tarik
TP de l’informatique industrielle

Digital = digitalRead (Digital_Input);

//... and output at this position


Serial.print ("Analog voltage value:"); Serial.print (Analog, 4);
Serial.print ("V, ");
Serial.print ("Limit value:");

if(Digital==1)
{
Serial.println (" reached");
}
else
{
Serial.println (" not yet reached");
}
Serial.println ("-------------------------------------------------
---------------");
delay (200);
}

Activity 23: FLAME-SENSOR MODULE (KY-026):


Description:
This module can be used to detect open flames. The attached photodiode is sensitive to the
spectral range of light generated by open flames.
Digital output: If a flame is detected, a signal is output here.
Analog output: Direct measured value of the sensor unit.
LED1: Indicates that the sensor is supplied with voltage.
LED2: Indicates that a flame has been detected.

Pr Jarou Tarik
TP de l’informatique industrielle

Pin assignment:

Montage :

The program reads the current voltage value which can be measured at the analog output and
outputs it to the serial interface.
In addition, the state of the digital pin is also indicated in the console, which means whether the
value has fallen below the limit or not.
Code:
// Declaration and initialization of the input pins
int Analog_Input = A0; // Analog output of the sensor

Pr Jarou Tarik
TP de l’informatique industrielle

int Digital_Input = 3; // Digital output of the sensor

void setup ()
{
pinMode (Analog_Input, INPUT);
pinMode (Digital_Input, INPUT);

Serial.begin (9600); // Serial output with 9600 bps


}

// The program reads the current values of the input pins


// and outputs them on the serial output
void loop ()
{
float Analog;
int Digital;

//Actual values are read, converted to the voltage value....


Analog = analogRead (Analog_Input) * (5.0 / 1023.0);
Digital = digitalRead (Digital_Input);

//... and output at this position


Serial.print ("Analog voltage value:"); Serial.print (Analog, 4);
Serial.print ("V, ");
Serial.print ("Limit value:");

if(Digital==1)
{
Serial.println (" reached");
}

Pr Jarou Tarik
TP de l’informatique industrielle

else
{
Serial.println (" not yet reached");
}
Serial.println ("-------------------------------------------------
---------------");
delay (200);
}

Activity 24: RGB 5MM LED MODULE (KY-016):


Description:
LED module which contains a red, blue and green LED. These are connected
to each other by means of a common cathode.

Pin assignment:

Montage :

Pr Jarou Tarik
TP de l’informatique industrielle

Code:
This code example shows how the integrated LEDs can be changed alternately, in 3 second
cycles, by means of a definable output pin.
int Led_Red = 10;
int Led_Green = 11;
int Led_Blue = 12;

void setup ()
{
// Initialize output pins for the LEDs
pinMode (Led_Red, OUTPUT);
pinMode (Led_Green, OUTPUT);
pinMode (Led_Blue, OUTPUT);
}

void loop () //Main program loop


{
digitalWrite (Led_Red, HIGH); // LED is switched on
digitalWrite (Led_Green, LOW); // LED is switched on
digitalWrite (Led_Blue, LOW); // LED is switched on
delay (3000); // Wait mode for 3 seconds

digitalWrite (Led_Red, LOW); // LED is switched on


digitalWrite (Led_Green, HIGH); // LED is switched on
digitalWrite (Led_Blue, LOW); // LED is switched on
delay (3000); // Waiting mode for another three seconds in which
the LEDs are then switched over

digitalWrite (Led_Red, LOW); // LED is switched on


digitalWrite (Led_Green, LOW); // LED is switched on
digitalWrite (Led_Blue, HIGH); // LED is switched on

Pr Jarou Tarik
TP de l’informatique industrielle

delay (3000); // Wait mode for another three seconds in which the
LEDs are then switched over
}

Activity 25: Microphone Sensor Module (KY-037):


Description:
This sensor emits a signal if the front microphone of the sensor perceives a noise. Sensitivity
of the sensor can be adjusted by means of a controller.
Digital output: Via the potentiometer, a limit value for the received sound can be set, at which
the digital output should switch.
Analog output: Direct microphone signal as voltage level.
LED1: Indicates that the sensor is powered.
LED2: Indicates that a noise has been detected.

PIN Assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage:

Code:
The program reads the current voltage value, which can be measured at the analog output, and
outputs it on the serial port.
In addition, the status of the digital pin in the console is also indicated, which means whether
the limit has been exceeded or not.
// Declaration and initialization of input pins
int Analog_Input = A0; // Analog output of the sensor
int Digital_Input = 3; // Digital output of the sensor

void setup ( )

Pr Jarou Tarik
TP de l’informatique industrielle

{
pinMode (Analog_In, INPUT);
pinMode (Digital_Inbox, INPUT);

Serial.begin (9600) ; // Serial output with 9600 bps


}

// The program reads the current values of the input pins


// and output it on the serial output
void loop ( )
{
float Analog;
int Digital;

//Actual values are read out, converted to the voltage value...


Analog = analogRead (Analog_In) * (5.0 / 1023.0);
Digital = digitalRead (Digital_Inbox) ;

//... and issued at this point


Serial.print ("Analog voltage value:"); Serial.print (Analog,
4) ; Serial.print (“V, “);
Serial.print (“Limit:”) ;

if (Digital==1)
{
Serial.println (“reached”);
}
else
{
Serial.println (“not reached yet”);

Pr Jarou Tarik
TP de l’informatique industrielle

}
Serial.println ( " ----------------------------------------------
------------------") ;
delay (200) ;
}

Activity 26: TEMPERATURE SENSOR MODULE (KY-028):


Description:
This module contains an NTC thermistor which can measure temperatures in the range of -55°C
up to +125°C. This has a decreasing resistance value at higher temperature.
Digital output: If a temperature is measured above a limit value, this is output here - the limit
value can be set using the potentiometer.
Analog output: Direct measured value of the sensor unit
LED1: Indicates that the sensor is supplied with voltage.
LED2: Indicates that the limit value has been exceeded

PIN Assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage :

The program reads the current voltage value which can be measured at the analog output and
outputs it to the serial interface.
In addition, the state of the digital pin is also indicated in the console, which means whether the
value has fallen below the limit or not.

Pr Jarou Tarik
TP de l’informatique industrielle

Code:

Activity 27: 2-COLOR (RED+GREEN) 3MM LED MODULE (KY-029):


Description:
LED module which contains a red and green LED. These are connected to each other by means
of a common cathode.

Pr Jarou Tarik
TP de l’informatique industrielle

PIN Assignment:

Montage:

Pr Jarou Tarik
TP de l’informatique industrielle

This code example shows how the integrated LEDs can be changed alternately, every 3 seconds,
by means of a definable output pin.
Code:

Activity 28: KNOCK-SENSOR MODULE (KY-031):


Description:
If the sensor is exposed to a knock/shock, the two output pins are short-circuited.

PIN Assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage:

This is an example program that makes an LED light up when a signal is detected on the sensor.
For example, the modules KY-011, KY-016 or KY-029 can be used as LEDs.

Pr Jarou Tarik
TP de l’informatique industrielle

Code:

Activity 29: OBSTACLE-DETECT MODULE (KY-032):


Description:
This sensor uses infrared light to detect obstacles. If the emitted infrared light hits an obstacle,
it is reflected and detected by the photodiode. The distance to be reached for detection can be
adjusted with the two knobs.
This behavior can be used in controllers, such as those used in robots, to be able to stop
autonomously if they were going to hit an obstacle.

Pr Jarou Tarik
TP de l’informatique industrielle

PIN Assignment:

Montage:

Pr Jarou Tarik
TP de l’informatique industrielle

The program reads the current state of the sensor pins and displays in the serial console whether
the obstacle detector is currently in front of an obstacle or not.

Code:

Activity 30: TRACKING SENSOR MODULE (KY-033):


Description:
The sensor module detects whether there is a light-reflecting or light-absorbing surface in front
of the sensor. What is currently the case, the module outputs at its digital output, as shown in
the pictures below. The sensitivity (resulting minimum distance) of the sensor can be regulated
with the controller.
This behavior can be used in controllers, such as those used in robots, in order to be able to
follow a line autonomously.

Pr Jarou Tarik
TP de l’informatique industrielle

PIN Assignment:

Montage :

Pr Jarou Tarik
TP de l’informatique industrielle

The program reads the current state of the sensor pin and displays in the serial console whether
the line tracker is currently on the line or not
Code:

Activity 31: 7 COLOUR LED FLASH-MODULE (KY-034):


Description:
If this module is powered, a sequence of color changes is automatically emitted from the LED,
which includes 7 different colors as well as all mixed colors of the 7 different colors included.

Pr Jarou Tarik
TP de l’informatique industrielle

PIN Assignment:

Montage :

Pr Jarou Tarik
TP de l’informatique industrielle

This code example shows how an LED can be switched on for four seconds and then switched
off for two seconds by means of a definable output spin.

Code:

Activity 32: BIHOR MAGNETIC SENSOR MODULE (KY-035):


Description:
The AH49E is a small, versatile and linear reverb device powered by the magnetic field of a
permanent magnet or an electromagnet. The output voltage is adjusted by the supply voltage
and varies proportional to the strength of the magnetic field. The integrated circuit is
characterized by a low output noise, which eliminates the need for external filtering. It has
precision resistors for increased temperature stability and accuracy. The operating temperature
range of these linear hall sensors is -40°C to +85 °C, suitable for commercial, private and
industrial applications.
The sensor emits an analog voltage signal via its output, which indicates the strength of the
magnetic field.

Pr Jarou Tarik
TP de l’informatique industrielle

PIN Assignment:

Montage :

Pr Jarou Tarik
TP de l’informatique industrielle

The program measures the current voltage value on the sensor, calculates the current resistance
value of the sensor from this and the known series resistor, and outputs the results on the serial
output.
Code:

Activity 33: METAL-TOUCH SENSOR MODULE (KY-036):


Description:
This sensor emits a signal if the front metal tip of the sensor is touched (in this case a touch
with the finger is meant). Sensitivity of the sensor can be adjusted by means of a controller.
Digital output: If a touch is detected, a signal is output.
Analog output: Direct measurement value of the sensor unit.
LED1: Indicates that the sensor is powered.
LED2: Shows that a touch has been detected.

Pr Jarou Tarik
TP de l’informatique industrielle

Pin assignment:

Montage :

Pr Jarou Tarik
TP de l’informatique industrielle

The program reads the current voltage value, which can be measured at the analog output, and
outputs it on the serial port.
In addition, the status of the digital pin in the console is also indicated, which means whether
the limit has been exceeded or not.
Code:

Pr Jarou Tarik
TP de l’informatique industrielle

Activity 34: MICROPHONE SOUND SENSOR MODULE (KY-038):


Description:
This sensor emits a signal if the front microphone of the sensor perceives a noise. Sensitivity
of the sensor can be adjusted by means of a controller.
Digital output: Via the potentiometer, a limit value for the received sound can be set, at which
the digital output should switch.
Analog output: Direct microphone signal as voltage level.
LED1: Shows that the sensor is powered.
LED2: Indicates that a noise has been detected.

PIN assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage:

Code:
The program reads the current voltage value, which can be measured at the analog output, and
outputs it on the serial port.
In addition, the status of the digital pin in the console is also indicated, which means whether
the limit has been exceeded or not.
// Declaration and initialization of input pins
int Analog_Input = A0; // Analog output of the sensor
int Digital_Input = 3; // Digital output of the sensor

void setup ( )
{
pinMode (Analog_In, INPUT);
pinMode (Digital_Input, INPUT);

Pr Jarou Tarik
TP de l’informatique industrielle

Serial.begin (9600) ; // Serial output with 9600 bps


}

// The program reads the current values of the input pins


// and outputs it on the serial output
void loop ( )
{
float Analogous;
int Digital;

//Current values are read out, converted to the voltage value...


Analog = analogRead (Analog_In) * (5.0 / 1023.0);
Digital = digitalRead (Digital_Input) ;

//... and issued at this point


Serial.print ("Analog voltage value:"); Serial.print (Analog,
4) ; Serial.print ("V, ");
Serial.print ("Limit value:") ;

if (Digital==1)
{
Serial.println (“reached”);
}
else
{
Serial.println (" not yet reached");
}
Serial.println ( " ----------------------------------------------
------------------") ;
delay (200) ;
}

Pr Jarou Tarik
TP de l’informatique industrielle

Activity 35: Heartbeat Sensor Module (KY-039):


Description:
If a finger is held between the infrared light-emitting diode and the photo transistor, the pulse
can be detected at the signal output.

PIN Assignment:

Montage:

Pr Jarou Tarik
TP de l’informatique industrielle

Code:
////////////////////////////////////////////////////
//////////////////////
/// Copyright (c) 2015 Dan Truong
/// Permission is granted to use this software under the MIT
/// license, with my name and copyright kept in source code
/// http: // http: //opensource.org/licenses/MIT
///
/// KY039 Arduino Heartrate Monitor V1.0 (April 02, 2015)
////////////////////////////////////////////////////
//////////////////////

// German Comments by Joy-IT

////////////////////////////////////////////////////
//////////////////////
/// @param [in] IRSensorPin Analog PI to which the sensor is
connected
/// @param [in] delay (msec) The delay between calling up the
sampling function.

Pr Jarou Tarik
TP de l’informatique industrielle

// You get the best results when you scan 5 times per heartbeat.
/// Not slower than 150mSec for e.g. 70 BPM pulse
/// 60 mSec would be better for e.g. up to a pulse of 200 BPM.
///
/// @ short description
/// This code represents a so-called peak detection.
/// No heartbeat history is recorded, but it
/// a search is made for "peaks" within the recorded data,
/// and indicated by LED. By means of the well-known delay
intervals, you can
/// the pulse can be roughly calculated.
////////////////////////////////////////////////////
//////////////////////

int rawValue;

bool heartbeatDetected (int IRSensorPin, int delay)


{
static int maxValue = 0;
static bool isPeak = false;

bool result = false;

rawValue = analogRead (IRSensorPin);


// Here the current voltage value at the photo transistor is read
out and stored temporarily in the rawValue variable
rawValue * = (1000 / delay);

// Should the current value deviate too far from the last maximum
value

Pr Jarou Tarik
TP de l’informatique industrielle

// (e.g. because the finger was put on again or taken away)


// So the MaxValue is reset to get a new base.
if (rawValue * 4L <maxValue) {maxValue = rawValue * 0.8; } //
Detect new peak
if (rawValue> maxValue - (1000 / delay)) {
// The actual peak is detected here. Should a new RawValue be
bigger
// as the last maximum value, it will be recognized as the top
of the recorded data.
if (rawValue> maxValue) {
maxValue = rawValue;
}
// Only one heartbeat should be assigned to the recognized peak
if (isPeak == false) {
result = true;
}
isPeak = true;
} else if (rawValue <maxValue - (3000 / delay)) {
isPeak = false;
// This is the maximum value for each pass
// slightly reduced again. The reason for this is that
// not only the value is otherwise always stable with every
stroke
// would be the same or smaller, but also,
// if the finger should move minimally and thus
// the signal would generally become weaker.
maxValue - = (1000 / delay);
}
return result;
}

Pr Jarou Tarik
TP de l’informatique industrielle

////////////////////////////////////////////////////
//////////////////////
// Arduino main code
////////////////////////////////////////////////////
//////////////////////
int ledPin = 13;
int analogPin = 0;

void setup ()
{
// The built-in Arduino LED (Digital 13) is used here for output
pinMode (ledPin, OUTPUT);

// Serial output initialization


Serial.begin (9600);
Serial.println ("Heartbeat detection sample code.");
}

const int delayMsec = 60; // 100msec per sample

// The main program has two tasks:


// - If a heartbeat is recognized, the LED flashes briefly
// - The pulse is calculated and output on the serial output.

void loop ()
{
static int beatMsec = 0;
int heartRateBPM = 0;
if (heartbeatDetected (analogPin, delayMsec)) {
heartRateBPM = 60000 / beatMsec;
// LED output at heartbeat

Pr Jarou Tarik
TP de l’informatique industrielle

digitalWrite (ledPin, 1);

// Serial data output


Serial.print ("Pulse detected:");
Serial.println (heartRateBPM);

beatMsec = 0;
} else {
digitalWrite (ledPin, 0);
}
delay (delayMsec);
beatMsec + = delayMsec;
}

Activity 36: Rotary Encoder (KY-040):


Description:
When the rotary switch is moved, the direction of movement and the current position of the
rotary switch are coded and output via the outputs and can therefore be used in other
applications.

PIN Assignment:

Pr Jarou Tarik
TP de l’informatique industrielle

Montage:

If the pin status has changed, the program checks which of the two pins changed first, which
indicates the direction of rotation. This information is obtained by comparing one of the two
pin values from a previous run with the value of the current run.
After the direction has been determined, the steps from the starting position are counted and
output. Pressing the rotary encoder button resets the current position.
** For the serial output: baud rate = 115200 **
Code:
// Initialization of the required variables
int counter = 0;
boolean direction;
int Pin_clk_Last;

Pr Jarou Tarik
TP de l’informatique industrielle

int Pin_clk_Aktuell;

// Definition of the input pins


int pin_clk = 3;
int pin_dt = 4;
int button_pin = 5;

void setup ()
{
// input pins are initialized ...
pinMode (pin_clk, INPUT);
pinMode (pin_dt, INPUT);
pinMode (button_pin, INPUT);

// ... and their pull-up resistors activated


digitalWrite (pin_clk, true);
digitalWrite (pin_dt, true);
digitalWrite (button_pin, true);

// Initial reading of the Pin_CLK


Pin_clk_last = digitalRead (pin_clk);
Serial.begin (115200);
}

// If the pin status has changed, the program checks which of the
two
// Pins changed first, which indicates the direction of rotation.
// This information is obtained by taking one of the two pin values
from a previous
// Compare pass with the value of the current pass.

Pr Jarou Tarik
TP de l’informatique industrielle

// After the direction has been determined, the steps are counted
and output from the start position.
// Pressing the rotary encoder button resets the current position.

void loop ()
{
// Read out the current status
Pin_clk_Aktuell = digitalRead (pin_clk);

// Check for change


if (Pin_clk_Aktuell! = Pin_clk_Lieter)
{

if (digitalRead (pin_dt)! = Pin_clk_Aktuell)


{
// Pin_CLK changed first
Counter ++;
Direction = true;
}

else
{// Otherwise, Pin_DT changed first
Direction = false;
Counter--;
}
Serial.println ("Rotation detected:");
Serial.print ("Direction of rotation:");

if (direction)
{

Pr Jarou Tarik
TP de l’informatique industrielle

Serial.println ("clockwise");
}
else
{
Serial.println ("counterclockwise");
}

Serial.print ("Current position:");


Serial.println (Counter);
Serial.println ("------------------------------");

// Preparation for the next run:


// The value of the current run is the previous value for the
next run
Pin_clk_Lieter = Pin_clk_Aktuell;

// Reset function to save the current position


if (! digitalRead (button_pin) && Counter! = 0)
{
Counter = 0;
Serial.println ("position reset");
}

Pr Jarou Tarik
TP de l’informatique industrielle

Activity 37: Ultrasonic Distance sensor (KY-050):


Description:
This sensor is used for distance measurement via ultrasound. If a signal (falling edge) is entered
at the trigger input, a distance measurement is carried out and output as a PWM-TTL signal at
the echo output.

Technical specifications :

PIN Assignment:

Montage:

Pr Jarou Tarik
TP de l’informatique industrielle

The example program activates the distance measurement according to the above principle and
measures the time with the help of the Arduino function pulseIn how long the ultrasonic signal
is in the air. This time is then used as the basis for converting the distance - the result is then
output in the serial output. If the signal is outside the measuring range, a corresponding error
message is issued.
Code:
#define Echo_EingangsPin 7 // Echo input pin
#define Trigger_AusgangsPin 8 // Trigger output pin

// Required variables are defined


int maximumRange = 300;
int minimumRange = 2;
long distance;
long duration;

void setup () {
pinMode (Trigger_AusgangsPin, OUTPUT);
pinMode (Echo_EingangsPin, INPUT);
Serial.begin (9600);
}

Pr Jarou Tarik
TP de l’informatique industrielle

void loop () {

// Distance measurement is started by means of the 10us long


trigger signal
digitalWrite (Trigger_AusgangsPin, HIGH);
delayMicroseconds (10);
digitalWrite (Trigger_AusgangsPin, LOW);

// Now we wait at the echo input until the signal has been
activated
// and then the time measured how long it remains activated
Duration = pulseIn (Echo_EingangsPin, HIGH);

// Now the distance is calculated using the recorded time


Distance = duration / 58.2;

// Check whether the measured value is within the permissible


distance
if (distance> = maximumRange || distance <= minimumRange)
{
// If not, an error message is output.
Serial.println ("Distance outside the measuring range");
Serial.println ("-----------------------------------");
}

else
{
// The calculated distance is output in the serial output
Serial.print ("The distance is:");
Serial.print (distance);

Pr Jarou Tarik
TP de l’informatique industrielle

Serial.println ("cm");
Serial.println ("-----------------------------------");
}
// Pause between the individual measurements
delay (500);
}

Activity 38: Voltage Translator / Level Shifter (KY-051):


Description:
This level shifter converts digital signals from one voltage to another up or down. There are 4
available channels that can be converted.
if you want to communicate between two systems with two different voltage levels (like the
Arduino -> Raspberry Pi example below), the voltage level must be "shifted" - if this is not
done, the system with the lower voltage level must transfer the excess voltage to the Input levels
"consume in heat". Depending on the system, this can lead to permanent damage to the system.

PIN Assignment:
The pin assignment is printed on the module board.
The signals at the inputs / outputs A1-A4 and B1-B4 are shifted to the respective voltage level
(VCCa -> A1-A4 | VCCb -> B1-B4)

Pr Jarou Tarik
TP de l’informatique industrielle

Example connection assignment between Arduino and Raspberry Pi:


PIN ASSIGNMENT ARDUINO:

PIN ASSIGNMENT RASPBERRY PI :

Please make sure that both systems are connected to the same GND (ground) - OE does not
have to be connected with this module.

Pr Jarou Tarik
TP de l’informatique industrielle

Example connection assignment between Arduino and Micro: Bit:


PIN ASSIGNMENT ARDUINO:

PIN ASSIGNMENT RASPBERRY PI :

Please make sure that both systems are connected to the same GND (ground) - OE does not have to
be connected to this module. A breakout board is recommended to connect the Micro: Bit.

Activity 39: Pressure sensor / Temperature sensor (KY-052):


Description:
This pressure sensor measures the air pressure at the sensor output (small hole on the silver
sensor housing) and outputs the result coded on the I2C bus.

A corresponding library is required for this module - see code examples below.

Pr Jarou Tarik
TP de l’informatique industrielle

TECHNICAL SPECIFICATIONS:

PIN Assignment:

This sensor allows it to be connected to and operated on both 5V and 3.3V systems. Please note
that only one of the respective power supply pins is connected; suitable for the voltage level of
the system used - see the examples below for connecting the Arduino (5V) or the Rasperry Pi
(3.3V).
Montage:

Pr Jarou Tarik
TP de l’informatique industrielle

This sensor does not output its measurement result as a signal on an output pin, but
communicates it via the I2C bus. This can be used to control the sensor and to start and evaluate
the respective measurements for pressure and temperature.
There are several ways to control this sensor module - the Adafruit_BMP280 library has proven
to be particularly accessible, which is provided by Adafruit published under the OpenSource
BSD license.
Code:
/ *************************************************
**************************
This is a library for the BMP280 humidity, temperature & pressure
sensor

Designed specifically to work with the Adafruit BMEP280 Breakout


----> http://www.adafruit.com/products/2651

These sensors use I2C or SPI to communicate, 2 or 4 pins are


required
to interface.

Adafruit invests time and resources providing this open source


code,

Pr Jarou Tarik
TP de l’informatique industrielle

please support Adafruit and open-source hardware by purchasing


products
from Adafruit!

Written by Limor Fried & Kevin Townsend for Adafruit Industries.


BSD license, all text above must be included in any redistribution
**************************************************
************************* /

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK 13
#define BMP_MISO 12
#define BMP_MOSI 11
#define BMP_CS 10

Adafruit_BMP280 bmp; // I2C


// Adafruit_BMP280 bmp (BMP_CS); // hardware SPI
// Adafruit_BMP280 bmp (BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);

void setup () {
Serial.begin (9600);
Serial.println (F ("BMP280 test"));

if (! bmp.begin ()) {
Serial.println (F ("Could not find a valid BMP280 sensor, check
wiring!"));
while (1);

Pr Jarou Tarik
TP de l’informatique industrielle

}
}

void loop () {
Serial.print (F ("Temperature ="));
Serial.print (bmp.readTemperature ());
Serial.println ("* C");

Serial.print (F ("Pressure ="));


Serial.print (bmp.readPressure ());
Serial.println ("Pa");

Serial.print (F ("Approx altitude ="));


Serial.print (bmp.readAltitude (1013.25)); // this should be
adjusted to your local forcase
Serial.println ("m");

Serial.println ();
delay (2000);
}

Activity 40: Analog Digital Converter (KY-053):


Description:
With the appropriate commands on the I2C bus, analog voltage values can be measured with
up to 16-bit accuracy on up to 4 inputs. The measurement result is coded and output on the I2C
bus.

Pr Jarou Tarik
TP de l’informatique industrielle

PIN Assignment:
The pin assignment is printed on the module board

Montage:

Pr Jarou Tarik
TP de l’informatique industrielle

The Arduino boards have a 10-bit ADC with 6 channels. However, if you need more channels
or higher accuracy, you can expand the Arduino by 4 ADC channels with 12-bit accuracy using
the KY-053 Analog Digital Converter Module, which is connected to the Arduino via I2C.
Code:

#include "ADS1x15.h"
#include <math.h>

// ADS1115 module is initialized - all subsequent operations with


the ADC
// can be executed with the help of the "ads" object.
Adafruit_ADS1115 ads;

void setup (void)


{
Serial.begin (9600);

Serial.println ("Values of the analog inputs of the ADS1115


(A0..A3) are read out and output");
Serial.println ("ADC Range: +/- 6.144V (1 bit = 0.1875mV)");

Pr Jarou Tarik
TP de l’informatique industrielle

// This module has signal amplifiers at its analog inputs, whose


// Gain can be configured via software in the areas below
// can.
// This is required when a certain voltage range
// is expected as the measurement result and thus a higher
resolution of the signal
// receives.
// Gain = [2/3] is selected as the standard gain and can be
commented out
// be switched to a different gain.
// ADS1115
// -------
ads.setGain (GAIN_TWOTHIRDS); // 2 / 3x gain +/- 6.144V 1 bit =
0.1875mV
// ads.setGain (GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 0.125mV
// ads.setGain (GAIN_TWO); // 2x gain +/- 2.048V 1 bit = 0.0625mV
// ads.setGain (GAIN_FOUR); // 4x gain +/- 1.024V 1 bit =
0.03125mV
// ads.setGain (GAIN_EIGHT); // 8x gain +/- 0.512V 1 bit =
0.015625mV
// ads.setGain (GAIN_SIXTEEN); // 16x gain +/- 0.256V 1 bit =
0.0078125mV

ads.begin ();
}

void loop (void)


{
uint16_t adc0, adc1, adc2, adc3;
float voltage0, voltage1, voltage2, voltage3;
float gain_conversion_factor;

Pr Jarou Tarik
TP de l’informatique industrielle

// The "ads.readADC_SingleEnded (0)" command is the actual


operation that starts the measurement in the ADC.
// the "0" as a variable for this function defines the channel
used that is to be measured
// If, for example, the third channel is to be measured, this must
be replaced with the "3"
adc0 = ads.readADC_SingleEnded (0);
adc1 = ads.readADC_SingleEnded (1);
adc2 = ads.readADC_SingleEnded (2);
adc3 = ads.readADC_SingleEnded (3);

// This value is required for the conversion into a voltage - this


depends on the set amplification.
// The appropriate value for the gain should be taken from the
table above
gain_conversion_factor = 0.1875;

// Conversion of the recorded values into a voltage


voltage0 = (adc0 * gain_conversion_factor);
voltage1 = (adc1 * gain_conversion_factor);
voltage2 = (adc2 * gain_conversion_factor);
voltage3 = (adc3 * gain_conversion_factor);

// Output of the values to the serial interface


Serial.print ("Analog input 0:"); Serial.print (voltage0);
Serial.println ("mV");
Serial.print ("Analog input 1:"); Serial.print (voltage1);
Serial.println ("mV");
Serial.print ("Analog input 2:"); Serial.print (voltage2);
Serial.println ("mV");
Serial.print ("Analog input 3:"); Serial.print (voltage3);
Serial.println ("mV");
Serial.println ("------------------------");

Pr Jarou Tarik
TP de l’informatique industrielle

delay (1000);
}

Pr Jarou Tarik

You might also like