... | @@ -11,6 +11,7 @@ For this assignment I use one potentiometer and an ultrasonic distance sensor as |
... | @@ -11,6 +11,7 @@ For this assignment I use one potentiometer and an ultrasonic distance sensor as |
|
- one LCD-Display (I2C)
|
|
- one LCD-Display (I2C)
|
|
- one microcontroller (Arduino or Raspberry Pi)
|
|
- one microcontroller (Arduino or Raspberry Pi)
|
|
- one breadboard
|
|
- one breadboard
|
|
|
|
- resistor (around 300 Ohm for the LED)
|
|
- cables
|
|
- cables
|
|
|
|
|
|
### Tinkercad
|
|
### Tinkercad
|
... | @@ -20,6 +21,7 @@ In Tinkercad I started with the starter set for the ultrasonic distance sensor. |
... | @@ -20,6 +21,7 @@ In Tinkercad I started with the starter set for the ultrasonic distance sensor. |
|
FOTOS
|
|
FOTOS
|
|
|
|
|
|
The code needed to be altered a little as the pins now changed. Instead of pin 7 for both inputs, I used pin 6 as the trigger pin and pin 7 as the echo pin. This needed to be changed in the function-call.
|
|
The code needed to be altered a little as the pins now changed. Instead of pin 7 for both inputs, I used pin 6 as the trigger pin and pin 7 as the echo pin. This needed to be changed in the function-call.
|
|
|
|
|
|
```
|
|
```
|
|
// code from the Arduino example with changed pins
|
|
// code from the Arduino example with changed pins
|
|
int inches = 0;
|
|
int inches = 0;
|
... | @@ -46,7 +48,7 @@ void setup() |
... | @@ -46,7 +48,7 @@ void setup() |
|
void loop()
|
|
void loop()
|
|
{
|
|
{
|
|
// measure the ping time in cm
|
|
// measure the ping time in cm
|
|
cm = 0.01723 * readUltrasonicDistance(6, 7); // before the change the function call was (7,7)
|
|
cm = 0.01723 * readUltrasonicDistance(6, 7); // before the function call was (7,7)
|
|
// convert to inches by dividing by 2.54
|
|
// convert to inches by dividing by 2.54
|
|
inches = (cm / 2.54);
|
|
inches = (cm / 2.54);
|
|
Serial.print(inches);
|
|
Serial.print(inches);
|
... | @@ -56,3 +58,97 @@ void loop() |
... | @@ -56,3 +58,97 @@ void loop() |
|
delay(100); // Wait for 100 millisecond(s)
|
|
delay(100); // Wait for 100 millisecond(s)
|
|
}
|
|
}
|
|
```
|
|
```
|
|
|
|
|
|
|
|
With the ultrasonic distance sensor working the display was added as the next step. So far the values of the distance sensor are just written to the Serial monitor. I want these values to be written on the display. Again, I looked at the Tinkercad presets with an I2C display and then added the display and the code accordingly. The preset used the [Adafruit_LiquidCrystal](https://www.arduino.cc/reference/en/libraries/adafruit-liquidcrystal/) library which offers support for I2C displays. The library and code for the display was added and the output for the distance values was set to the display instead of the Serial monitor.
|
|
|
|
|
|
|
|
```
|
|
|
|
#include <Adafruit_LiquidCrystal.h>
|
|
|
|
|
|
|
|
int inches = 0;
|
|
|
|
int cm = 0;
|
|
|
|
String text = "";
|
|
|
|
Adafruit_LiquidCrystal lcd_display(0);
|
|
|
|
|
|
|
|
long readUltrasonicDistance(int triggerPin, int echoPin)
|
|
|
|
{
|
|
|
|
pinMode(triggerPin, OUTPUT);
|
|
|
|
digitalWrite(triggerPin, LOW);
|
|
|
|
delayMicroseconds(2);
|
|
|
|
digitalWrite(triggerPin, HIGH);
|
|
|
|
delayMicroseconds(10);
|
|
|
|
digitalWrite(triggerPin, LOW);
|
|
|
|
pinMode(echoPin, INPUT);
|
|
|
|
return pulseIn(echoPin, HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
lcd_display.begin(16,2);
|
|
|
|
lcd_display.print("Abstand:");
|
|
|
|
Serial.begin(9600);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
// measure the ping time in cm
|
|
|
|
cm = 0.01723 * readUltrasonicDistance(6, 7);
|
|
|
|
// convert to inches by dividing by 2.54
|
|
|
|
inches = (cm / 2.54);
|
|
|
|
lcd_display.setCursor(0,1);
|
|
|
|
// merge values and added text to a single string
|
|
|
|
text = String(cm) + " cm / " + String(inches) + " in";
|
|
|
|
lcd_display.print(text);
|
|
|
|
delay(100); // Wait for 100 millisecond(s)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Now that the display shows the distance values, the LED and the potentiometer are added. The LED was connected with a 299 ohm resistor and uses pin 11. It is important that the pin which the LED is connected to has PWM, as the input I want to control the LED with is an analog input. The potentiometer is connected to pin A0. In the code we are adding the respective pins, read the analog input from the potentiometer and map the values within a range of 0 and 255 for the digital LED output. The final code looks like this:
|
|
|
|
|
|
|
|
```
|
|
|
|
#include <Adafruit_LiquidCrystal.h>
|
|
|
|
|
|
|
|
int inches = 0;
|
|
|
|
int cm = 0;
|
|
|
|
String text = "";
|
|
|
|
int pot_val = 0;
|
|
|
|
int led = 11;
|
|
|
|
Adafruit_LiquidCrystal lcd_display(0);
|
|
|
|
|
|
|
|
long readUltrasonicDistance(int triggerPin, int echoPin)
|
|
|
|
{
|
|
|
|
pinMode(triggerPin, OUTPUT);
|
|
|
|
digitalWrite(triggerPin, LOW);
|
|
|
|
delayMicroseconds(2);
|
|
|
|
digitalWrite(triggerPin, HIGH);
|
|
|
|
delayMicroseconds(10);
|
|
|
|
digitalWrite(triggerPin, LOW);
|
|
|
|
pinMode(echoPin, INPUT);
|
|
|
|
return pulseIn(echoPin, HIGH);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
pinMode(A0, INPUT);
|
|
|
|
pinMode(led, OUTPUT);
|
|
|
|
lcd_display.begin(16,2);
|
|
|
|
lcd_display.print("Abstand:");
|
|
|
|
Serial.begin(9600);
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
pot_val = map(analogRead(A0), 0, 1023, 0, 255);
|
|
|
|
analogWrite(led, pot_val);
|
|
|
|
|
|
|
|
// measure the ping time in cm
|
|
|
|
cm = 0.01723 * readUltrasonicDistance(6, 7);
|
|
|
|
// convert to inches by dividing by 2.54
|
|
|
|
inches = (cm / 2.54);
|
|
|
|
lcd_display.setCursor(0,1);
|
|
|
|
// merge values and added text to a single string
|
|
|
|
text = String(cm) + " cm / " + String(inches) + " in";
|
|
|
|
lcd_display.print(text);
|
|
|
|
delay(100); // Wait for 100 millisecond(s)
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|