Experimente mit dem  Super Starterkit - Teil 5: Einfache Sensoren, Mikrofon, Ultraschall-Sensor - AZ-Delivery


This article is about the simple sensors contained in the set. These are the inclination sensor, the brightness sensor, the temperature sensor, and the rain sensor. In addition, the microphone module for switching using noises and the ultrasonic sensor for distance measurement is treated.

Required hardware

Number

Component

annotation

1

Super starter kit


1

9V block battery or 9V power supply 

optional


Inclination sensor SW520D

The inclination sensor is a very simple digital sensor. The two connection spins are connected in an upright position. If it is tilted more than around 45 °, this connection is interrupted.

For testing, a sketch is to be created that shines an LED when the connection in the inclination sensor is interrupted. The inclination sensor is connected between input 2 and GND. The internal pullup resistance is to be activated at entrance 2. This means that if the contact of the inclination sensor is closed, the voltage at the input 2 0V is otherwise 5V. The LED is switched via a 1kΩ resistance between exit 3 and GND. If the 5V is located at the output, the LED lights up. The sketch is very simple.

First, the pins used are defined again

#define sensor 2
#define LED 3

In the set up() Function is activated by the pull-up resistance for the input and the PIN 3 is defined as an output.
void setup () {
Pinmode (sensor, input_pullup);
Pinmode (LED, output);
}
In the Loop () Function simply reads the condition of the entrance and output on the output.
void loop () {
DigitalWrite (LED, Digital Read (Sensor));
}

In the test, you will see that the LED does not illuminate at 45 degrees. This is mainly due to adhesion. This causes the balls to stick together. On the other hand, the inclination sensor also reacts to vibrations.

Temperature sensor NTC

The temperature sensor is a resistance with strong temperature dependency and a negative temperature coefficient. This means that the resistance value decreases with increasing temperature. In order to be able to measure the temperature with the sensor, it is used as part of a voltage divider. Since the nominal resistance is 10kΩ, it makes sense to use 10kΩ as a second resistance for the voltage divider.


The voltage on A0 depends on the NTC resistance Rt. It results from the formula
UA0 = 5V / (rT + 10kΩ) * 10kΩ
This gives the following characteristic for the temperature depending on the measured voltage.


You can see the curve is not linear. However, if you look at a limited temperature range between -20 ° C and 60 ° C, the curve can be viewed as approximately linearly.


The slope of the straight is 18.8 ° C/v. At 0 ° C, the voltage is 1.17V, so the temperature is obtained from the measured voltage uT
T = (uT - 1,17) * 18,8

The sketch for testing spends the temperature on the serial interface in a second interval.
First, the input used and the reference voltage for the analog-digital converter is defined.

#define sensor A0
#define reference 5.0
In the set up() Function is activated by the serial interface and the PIN used is defined as input
void setup () {
Serial.begin (9600);
Pinmode (sensor, input);
}

In the Loop () Function is first read from the sensor input and converted into volts using the reference voltage. The analog-digital converter has a resolution of 10-bit. Its value range, therefore, goes from 0 to 1023. That means a value of 1023 corresponds to 5V. It, therefore, applies U = value / 1023 * 5V. Then the voltage is reduced by the value at 0 ° C and multiplied by the slope. The result is issued to the serial interface. Then there is a break of 1000 ms.
void loop () {
float spg = analogread (sensor) / 1023.0 * reference;
float temp = (spg - 1.17) * 18.8;
Serial.print ("temperature =");
Serial.print (temp);
Serial.println ("° C");
Delay (1000);
}

If you want to take advantage of the whole temperature range, the logarithmic approximation must be used. β is the characteristic of the thermistor. In the present case, it is 3900. uRef is the reference voltage. It is 5V. TN is the node. It is 25 ° C. K is finally 0 ° C 273.15 ° K. This means the formula:

T = β / (β / (k + tN) + LN (uN - u) / u)- K

Or with constants used

T = 3900 / (3900 / 298.15 + LN ((5 - u) / u)) - 273.15

In the sketch

float temp = 3900/(3900/298.15 + log ((reference - SPG)/SPG)) - 273.15;

With this formula, you get useful values ​​in the entire permissible temperature range of -50 ° C and +150 ° C.

Light sensor LDR

The Light Dependent Resistor (LDR) is a resistance, the resistance of which decreases with increasing lighting. An LDR consists of the semiconductor material Cadmium sulfide. Light radiation generates free charge carriers that enable a current flow. In order for a large length of the opposite electrodes to be reached, the electrodes are executed as a comb structure. The characteristic line is also non-linear at the LDR. The following characteristic line was determined via a test arrangement. A brightness sensor BH1750 served to measure the comparison measurement.

With the formula

E = e(U * 1.4) / 0.4 [lux]

You get a usable approach in the range between 0 and 1000 lux. The circuit is identical to the circuit for the temperature sensor

The sketch is also largely identical. Only the calculation of the measured value is different.

First, the input used and the reference voltage for the analog-digital converter is defined.

#define sensor A0
#define reference 5.0
In the set up() Function is activated by the serial interface and the PIN used is defined as input
void setup () {
Serial.begin (9600);
Pinmode (sensor, input);
}

In the Loop () Function is first read from the sensor input and converted into volts using the reference voltage. The analog-digital converter has a resolution of 10-bit. Its value range, therefore, goes from 0 to 1023. This means that a value of 1023 corresponds to 5V. It, therefore, applies U = value / 1023 * 5V. Then the illuminance is calculated with the formula specified above.
void loop () {
float spg = analogread (sensor) / 1023.0 * reference;
float lux = exp (spg * 1.4) / 0.4;
Serial.print ("Belecuhung =");
Serial.print (lux);
Serial.println ("lux");
Delay (1000);
}

Rain Sensor
The rain sensor is very simple. It consists of a circuit board with conductor tracks that run in parallel. The conductor tracks are gold-plated to protect against corrosion. If the conductor tracks come into contact with a conductive liquid, for example, water, a current can flow between the conductor tracks. A voltage is measured at the signal output. A drop of water is already sufficient to measure a voltage. The tension depends on the wetland and the conductivity of the liquid. An evaluation of the voltage height brings no benefit.

The circuit on the left shows the internal structure of the sensor.

The sketch should switch on the LED on PIN 2 when moisture is recognized on the sensor. The sensor should only be supplied with voltage for the measurement so that there is no corrosion due to electrolysis. Therefore, the plus connection of the sensor is connected to the PIN 3 of the Arduino. A0 is used as the input for the sensor voltage.

First, the input used and the outputs for the LED and for the power supply of the sensor are defined

#define sensor A0
#define LED 2
#define Power 3

In the set up() Function is activated by the serial interface and the pins used are adjusted to the corresponding operating mode.

void setup () {
Serial.begin (9600);
Pinmode (sensor, input);
Pinmode (LED, output);
Pinmode (power, output);
}

In the loop () function, the power supply for the sensor is first switched on. Then the voltage is read by the sensor and in the variable Val saved. Now the power supply can be switched off again. The measured value is issued to the serial interface. If the measured value is larger than 100, the LED will be switched on, otherwise switched off. Here the result of the comparison can be directly DigitalWrite (LED, VAL> 100); be output. 10 seconds follow the next measurement.

void loop () {
DigitalWrite (Power, 1);
uint16_t val = analogread (sensor);
DigitalWrite (Power, 0);
Serial.print ("measured value =");
Serial.println (val);
DigitalWrite (LED, VAL> 100);
Delay (10000);
}

microphone


The microphone module contains a sensitive electrical microphone and a comparator.

The microphone has an internal resistance of 1kΩ. The voltage on A0 is compared in the first comparator with half the operating voltage (voltage divider R6 and R2). In order to achieve high sensitivity, the potentiometer should also be set to half the operating voltage with the potentiometer. If the voltage on A0 is smaller than half the operating voltage, the output of the comparator and also D0 switch to the operating voltage. The second comparator also compares the voltage on D0 with half the operating voltage and therefore switches the output to GND. The LED L2 shines. The LED L1 shows the operating voltage. The sensor module is designed to detect noises.
If you need the possibility to change the threshold dynamically, you have to use the A0 connection. The D0 connection is more suitable for a fixed threshold.


In the example sketch, an LED is to be switched on and off by clapping hands. Both variants (analog or digital) are shown. For the analog variant, the connection A0 of the module is connected to A0 on the microcontroller (dotted green line). For the digital variant, connection D0 of the module is connected to connection A0 of the microcontroller (pulled green line).

The LED is connected to PIN 2 via a 1KΩ resistance.

The sketch for the digital variant

The potentiometer must be adjusted so that the LED does not light up next to the potentiometer, but flashes on noise.

First, the pins used are defined
#define sensor A0
#define LED 2


A global variable is required to save the condition of the LED
boolean led = 0;

The operating mode of the pins used in the set up() Function is set.

void setup () {
Pinmode (sensor, input);
Pinmode (LED, output);
}
In the Loop () Function becomes the condition of the input A0 had read. If the condition is HIGH, the condition of the LED is inverted and the new state is written on the PIN for the LED. So that a single sound does not become effective several times, a delay of one second is inserted. That means switching between the on and off and switching off for at least one second.

void loop () {
boolean on1 = digital read (sensor);
if (on1) {
LED =! LED;
DigitalWrite (LED, LED);
Delay (1000);
  }
}

The sketch for the analog variant

With this variant, the attitude of the potentiometer plays a subordinate role. However, it should be set roughly in such a way that approximately a 2.5 V module is measured.

The pins used are defined
#define sensor A0
#define LED 2


Two global variables are required. One for the current status of the LED and one for saving the rest voltage at the analog input.
boolean led = 0;
uint16_t ref = 0;

In the set up() Function is set the operating mode of the pins. Then the rest voltage at the analog input is as a reference value in the global variables ref saved.
void setup () {
Pinmode (sensor, input);
Pinmode (LED, output);
ref = analogread (sensor);
}


In the Loop () Function is measured on the current voltage at the analog input and in the variable Val saved. Now the absolute difference between the current voltage and the resting voltage is determined. This difference is in the variable delta saved. If the difference is larger than 20, a noise had occurred. The status of the LED is inverted and the new value is output on PIN 2. After that, a second wait to avoid multiple reactions.
void loop () {
uint16_t val = analogread (sensor);
int16_t Delta = ABS (REF-VAL);
if (delta> 20) {
LED =! LED;
DigitalWrite (LED, LED);
Delay (1000);
  }
}

Electronic candle
If you use an LDR for switching on and the microphone module to switch off, you can "light" and "blow out" LEDs. So you get an "electronic candle".

The LDR is connected to A1 and the microphone with its analog output to A0. The LED comes via a resistance of 1kΩ to pin 2. The sketch is very simple.

The pins used are defined
#define mik a0
#define LDR A1
#define LED 2


Two global variables are required to save the LED status and the reference voltage for the microphone.
boolean led = 0;
uint16_t ref = 0;


In the set up() Function is set the operating mode of the pins. Then the rest voltage at the analog input is as a reference value in the global variables ref saved.
void setup () {
Pinmode (mik, input);
Pinmode (LDR, input);
Pinmode (LED, output);
ref = analogread (mik);
}
In the Loop () Function takes place different processing depending on the status of the LED. If the LED is out, the program waits for the voltage to the LDR over a certain value. This happens when a bright light source, e.g. a burning match, is held in front of the LDR. If the threshold is exceeded, the status of the LED is set to high and output on PIN 2.
If the LED is on, the program waits for the voltage to deviate from the reference voltage at the analogue input. This happens when the microphone perceives a noise, e.g. by blowing on the microphone. The status of the LED is then placed on low and output on pin 2.
void loop () {
If (! LED) {
uint16_t val = analogread (LDR);
if (val> 800) {
LED = 1;
DigitalWrite (LED, LED);
    }
} Else {
uint16_t val = analogread (mik);
int16_t Delta = ABS (VAL - REF);
if (delta> 20) {
LED = 0;
DigitalWrite (LED, LED);
    }
  }
}

Ultrasound distance sensor
The distance sensor consists of an ultrasound broadcaster on the left and an ultrasound receiver on the right. The evaluation electronics are located on the back of the circuit board.

The supply voltage is 5V. The measurement begins if an impulse with a duration of at least 10µs is created at the trigger input. Eight vibrations with 40kHz are sent via the ultrasound transmitter. The electronics then determine the term until the echo arrives at the ultrasound recipient. After the measurement, an impulse is generated at the Echo output, the duration of which corresponds to the determined term. The speed of sound is 340 m/s or 34 cm/ms. Since the signal from the transmitter to the receiver has to cover the double distance, the signal requires a distance of 1 cm 2/34 ms or 2000/34 µs = 59 µs. Therefore, the distance is equal to the measured time in µs by 59. At least 60 ms should pass between the two measurements.

In the test arrangement, the trigger input is connected to PIN 3, and the Echo output to PIN 2. The sketch outputs an impulse of 15 µs every second on PIN 3. Then there is waiting for an impulse of pin 2 and its duration is measured. Exactly for this purpose, there is the function
Unsigned Long Pulsein (Uint8_t Pin, Uint8_t State)
The parameter pin code Contains the number of the pin on which the impulse occurs. The parameter state Is high or low. It indicates what condition should be serviced. The return value is the pulse duration in µs. This results in a very simple sketch to measure distances with the distance sensor.

First, the pins used are defined again.
#define Trigger 2
#define Echo 3


In the set up() Function is launched the serial interface, then the operating mode of the pins used is set and the trigger output is set to low.

void setup () {
Serial.begin (9600);
Pinmode (trigger, output);
Pinmode (echo, input);
DigitalWrite (Trigger, 0);
}

in the Loop () Function is set back to low and after a delay of 15 µs. Now follows the function Pulse in ()that returns the duration of the pulse at echo input in µs. If the returned value is larger than 38,000, there is no object in the permitted measuring range. A corresponding error message is issued to the serial interface. If the value is smaller than 38,000, you get the distance in cm with a division of 59. This distance is issued to the serial interface. Then wait a second before the next measurement begins.
void loop () {
DigitalWrite (trigger, high);
Delaymicroseconds (15);
DigitalWrite (trigger, low);
uint32_t echo = pulse (echo, high);
if (echo> 38000) {
Serial.println ("outside the measuring range");
} Else {
float distance = echo/59;
Serial.print (distance);
Serial.println ("cm");
  }
Delay (1000);
}

This describes all simple sensors contained in the set. The next part will follow the I2C bus, with which the intelligent Sensor DHT11 and the LCD display are controlled.

Post as a PDF

Für arduinoProjekte für anfängerSensoren

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. ESP32 jetzt über den Boardverwalter installieren - AZ-Delivery
  2. Internet-Radio mit dem ESP32 - UPDATE - AZ-Delivery
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1 - AZ-Delivery
  4. ESP32 - das Multitalent - AZ-Delivery