[{"data":1,"prerenderedAt":125},["ShallowReactive",2],{"example-page-uno-sonar":3},{"id":4,"info":5,"parts":6,"wired":17,"refs":18,"nets":19,"moreNets":119,"code":120},"uno-sonar","The HC-SR04 (TRIG D12, ECHO D11) measures the distance with pulseIn(). Five LEDs on D3–D7 form a bar: the closer the object, the more LEDs light (below 100, 70, 45, 25 and 10 cm). Change the distance of U2 in the inspector while the simulation runs; the serial console shows the value in cm.",[7,10,12,15],[8,9],"led",5,[11,9],"resistor",[13,14],"arduino_uno",1,[16,14],"hcsr04",false,{"A1":13,"D1":8,"D2":8,"D3":8,"D4":8,"D5":8,"U2":16,"R1":11,"R2":11,"R3":11,"R4":11,"R5":11},[20,39,45,51,59,66,72,77,84,89,96,101,108,113],{"name":21,"pins":22},"0",[23,26,29,31,33,35,37],[24,25],"A1","GND",[27,28],"D1","K",[30,28],"D2",[32,28],"D3",[34,28],"D4",[36,28],"D5",[38,25],"U2",{"name":40,"pins":41},"5V",[42,43],[24,40],[38,44],"VCC",{"name":46,"pins":47},"ECHO",[48,50],[24,49],"~D11\u002FMOSI",[38,46],{"name":52,"pins":53},"L1",[54,56],[24,55],"~D3",[57,58],"R1","1",{"name":60,"pins":61},"L1_A",[62,64],[27,63],"A",[57,65],"2",{"name":67,"pins":68},"L2",[69,70],[24,34],[71,58],"R2",{"name":73,"pins":74},"L2_A",[75,76],[30,63],[71,65],{"name":78,"pins":79},"L3",[80,82],[24,81],"~D5",[83,58],"R3",{"name":85,"pins":86},"L3_A",[87,88],[32,63],[83,65],{"name":90,"pins":91},"L4",[92,94],[24,93],"~D6",[95,58],"R4",{"name":97,"pins":98},"L4_A",[99,100],[34,63],[95,65],{"name":102,"pins":103},"L5",[104,106],[24,105],"D7",[107,58],"R5",{"name":109,"pins":110},"L5_A",[111,112],[36,63],[107,65],{"name":114,"pins":115},"TRIG",[116,118],[24,117],"D12\u002FMISO",[38,114],0,{"file":121,"lang":122,"text":123,"lines":124,"more":17},"sketch.ino","cpp","\u002F\u002F HC-SR04 (TRIG D12, ECHO D11) measures the distance; the LED bar on D3..D7 shows how close:\n\u002F\u002F more LEDs = closer (green, green, yellow, yellow, red)\nconst int TRIG = 12, ECHO = 11;\nconst int LEDS[] = { 3, 4, 5, 6, 7 };\nconst int STEPS[] = { 100, 70, 45, 25, 10 };               \u002F\u002F cm: LED k lights below STEPS[k]\n\nvoid setup() {\n  Serial.begin(115200);\n  pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT);\n  for (int k = 0; k \u003C 5; k++) pinMode(LEDS[k], OUTPUT);\n}\n\nvoid loop() {\n  digitalWrite(TRIG, LOW); delayMicroseconds(2);\n  digitalWrite(TRIG, HIGH); delayMicroseconds(10); digitalWrite(TRIG, LOW);\n  unsigned long us = pulseIn(ECHO, HIGH, 30000);            \u002F\u002F 30 ms: about 5 m\n  float cm = us \u002F 58.0;\n  for (int k = 0; k \u003C 5; k++) digitalWrite(LEDS[k], us > 0 && cm \u003C STEPS[k]);\n  Serial.print(F(\"Distance: \"));\n  if (us == 0) Serial.println(F(\"out of range\")); else { Serial.print(cm, 1); Serial.println(F(\" cm\")); }\n  delay(60);                                                \u002F\u002F let the echo die out\n}",22,1796304400919]