Hello, community,
today I would briefly like to show a primitive sonar with representation on a TFT display. The conversion is held intentionally easy and without corner correction. This example of use should point like one can realise in short time an optically very impressive project with little hardware. In our example a SR04 "scans" mounted an area of 180 ° on a SG90 Servo and we give recognised Hinternisse are economical effectively on our 1.77" TFT display.
Parts list:
1x Electricity supply (e.g., Kit MB102 Breadboard)
1x Servo SG90 with assembly parts
1x TFT display 1.77"
1x HC-SR04
1x Nano V3
Pinbelegung:
Electricity supply | NanoV3 | TFT display | SR04 | Servo |
5 V | 5 V | Pin 2 | VCC | red |
GND | GND | Pin 1 | GND | black |
3.3 V | Pin 8 | |||
D2 | Trig | |||
D3 | Echo | |||
D6 | yellow | |||
D8 | Pin 5 | |||
D9 | Pin 6 | |||
D10 | Pin 7 | |||
D11 | Pin 4 | |||
D13 | Pin 3 |
Sketch:
//Servo #include <Servo.h> Servo servo; //HC-SR04 int interval = 0; stands in distance = 0; //Prepare the TFT #include <TFT.h> //Arduino TFT library (state kind) #include <SPI.h>//Display Pins in the Nano #define cs 10 #define dc 9 #define rst 8//Authority for TFT TFT TFTscreen = TFT(Cs, dc, rst);//char Array char rc_Printout [4]; void set-up() {servo.attach(6); //D6 in the Nano pinMode( 2, OUTPUT ); pinMode( 3, INPUT ); Serial.begin(9600); TFTscreen.begin(); TFTscreen.background (0, 0, 0);} void loop() { int r_beam = 100; //D6 TFTscreen.stroke (255, 255, 255); TFTscreen.circle (80,128, r_beam 2); TFTscreen.setTextSize (2); TFTscreen.text ("Dist (cm)", 0, 0); //to the right turn for (int i=0;i<180;i=i 1) {servo.write(i); measure_dist ();Serial.print(i);Serial.print("\n"); delay(20); int r = distance*2;String r_Printout = String(r / 2); TFTscreen.stroke (0, 0, 0); TFTscreen.setTextSize (2); TFTscreen.text (rc_Printout, 100, 0); r_Printout.toCharArray(rc_Printout, 4); TFTscreen.stroke (255, 255, 255); TFTscreen.setTextSize (2); TFTscreen.text (rc_Printout, 100, 0); TFTscreen.stroke (70, 70, 70); TFTscreen.line (80,128.80 r_beam*cos((360-i) *3.14/180), 128 r_beam*sin((360-i) *3.14/180)); TFTscreen.stroke (0, 255, 0); TFTscreen.circle (80 r*cos((360-i) *3.14/180), 128 r*sin((360-i) *3.14/180), 2);} TFTscreen.background (0, 0, 0); TFTscreen.stroke (255, 255, 255); TFTscreen.circle (80,128, r_beam 2); TFTscreen.setTextSize (2); TFTscreen.text ("Dist (cm)", 0, 0); //to the left turn for (int i=180;i>0;i=i-1) {servo.write(i); measure_dist ();Serial.print(i);Serial.print("\n"); delay(20); int r = distance*2;String r_Printout = String(r / 2); TFTscreen.stroke (0, 0, 0); TFTscreen.setTextSize (2); TFTscreen.text (rc_Printout, 100, 0); r_Printout.toCharArray(rc_Printout, 4); TFTscreen.stroke (255, 255, 255); TFTscreen.setTextSize (2); TFTscreen.text (rc_Printout, 100, 0); TFTscreen.stroke (70, 70, 70); TFTscreen.line (80,128.80 r_beam*cos((360-i) *3.14/180), 128 r_beam*sin((360-i) *3.14/180)); TFTscreen.stroke (0, 255, 0); TFTscreen.circle (80 r*cos((360-i) *3.14/180), 128 r*sin((360-i) *3.14/180), 2);} TFTscreen.background (0, 0, 0);} void measure_dist () {Serial.Read(); //Trig SR04 digitalWrite( 2, HIGH ); delayMicroseconds( 100 ); digitalWrite( 2, LOW ); //Echo SR04 interval =pulseIn( 3, HIGH ); distance = interval * 0.017;//cm Serial.print(interval, DEC ); Serial.print( "\t" ); Serial.print(distance, 1);Serial.print( "\n" ); }
6 comments
Andreas Wolter
@Douglas Debnar:
Can you explain this in more detail? What is the error you are experiencing?
Douglas Debnar
The code that is posted is wrong. The for loops are missing some things. Pleas check k and repost.
Siebo Mörig
Moin.
Super Idee. Würdet Ihr das ganze noch um eine Fritzing Zeichnug erweitern? Auch ein kleines Video währe sehr anschaulich.
Macht weiter so.
Ist der direkte Betrieb des Servos über den Ausgang so sinnvoll? Oder sollte da besser noch ein Treiber zwischengeschaltet werden?
Vielen Dank
Siebo
Hans-Gerd Valks
Wie wäre es mit einer Säge?😜
Markus Neumann
Hallo Herr Rode,
unser Mini-Breadboard gibt es unter https://www.az-delivery.de/products/mini-breadboard
Heinrich Rode
Moin,
ich möchte das Sonar gerne nachbauen. Jetzt stellt sich die Frage, wo ich so ein kurzes Breadboard her bekomme.
Gruß Heiner