11/11/2023 0 Comments Motor shield for arduino kitWe trigger the sensor by setting A1 (TRIG) to LOW for 2 microseconds, then HIGH for 10 microseconds, and finally LOW again.It repeatedly sends out ultrasonic pulses and calculates the distance based on the time it takes for the pulse to bounce back. The loop() function is where the magic happens.A1 is set as an OUTPUT pin, connected to TRIG, while A0 is set as an INPUT pin, connected to ECHO. The setup() function defines the pin modes.We initialize the serial communication for debugging purposes using Serial.begin().Here’s a brief explanation of the code’s functionality: The ultrasonic sensor’s TRIG and ECHO pins are connected to analog pins A1 and A0, respectively. In this code, we utilize the Adafruit Motor Shield library to control the motors. ![]() Serial.println(distanceCm) // If the distance is less than or equal to 20cm, display it PinMode(A0, INPUT) // Analog pin A0 connected to ECHOĭigitalWrite(A1, HIGH) // Give a pulse of 10 microseconds on TRIGĭuration = pulseIn(A0, HIGH) // Check the time elapsed in receiving back the pulse on ECHOĭistanceCm = duration * 0.034 / 2 // Convert to distance in centimeters PinMode(A1, OUTPUT) // Analog pin A1 connected to TRIG Int distanceCm // Distance in centimeters Long duration // Duration of ultrasonic pulse Below is the Arduino sketch to make everything work: #include // Adafruit Motor Driver Shield library Now, let’s delve into the heart of the matter-the code that makes this setup come to life. Before you can use the Motor shield, you must install the AF_Motor Arduino library.Before you can harness the power of the Motor Shield, it’s essential to install the AF_Motor Arduino library.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |