• Link to Facebook
  • Link to X
  • Link to Youtube
  • Link to Instagram
  • Link to TikTok
  • Link to Mail
Code Red Robotics
  • Home
    • Frequently Asked Questions
    • Code Red Calendar
    • Blog/News
    • Contact Us!
  • About Our Teams
    • FIRST Robotics Competition (FRC)
      • Meet the Team
      • What We Do
      • Our Robots
      • FIRST Awards
    • FIRST Tech Challenge (FTC)
    • Next Robotics Generation (NRG)
    • FIRST Lego League Explore
  • Resources
    • Forms & Documents
    • Code Red Scrapbook
    • FRC Resources
    • FRC Controls Resources
    • NRG Controls Resources
    • Smart Robot Car Assembly Video
    • Bug Report
  • Donate to Code Red!
  • Click to open the search input field Click to open the search input field Search
  • Menu Menu

Ultrasonic Example #1

You are here: Home1 / Next Robotics Generation (NRG)2 / NRG Controls Resources3 / Ultrasonic Example #1

Ultrasonic Example #1

This is example #1 for using the robot’s ultrasonic sensor.  You can download the Arduino sketch (the .ino) file by clicking here.

Once downloaded, open the file in your Download folder, then save it to your “My Documents\Arduino” folder.  It will create a folder in the Arduino with the same name as the filename and then it will save the file in the new folder.

Example:

Filename:  “example.ino”

After saving in the Arduino folder the file will be located here:
“My Documents\Arduino\example\example.ino”

Code:

/*
  NRG Ultrasonic Example
  2019-09-24 R. Baas

  This example uses the robot car's ultrasonic sensor to sense the distance
  to a solid surface and to light the built-in LED on pin 13 when the
  distance is less than 15 cm.

*/

// Pin definitions for ultrasonic sensor.
int Echo = A4;  
int Trig = A5; 

// Variable definitions.
int distance;
int trigger_distance = 15;  // The distance below which the LED will light.

// Ultrasonic distance measurement function.
// Note this function starts with "int" and not "void" because
// it returns an integer value when called.
int Distance_test() {
  digitalWrite(Trig, LOW);  // This line and the next 4 tell the sensor to fire.
  delayMicroseconds(10);
  digitalWrite(Trig, HIGH);  
  delayMicroseconds(12);
  digitalWrite(Trig, LOW);   
  float Fdistance = pulseIn(Echo, HIGH);  // pulseIn gives the ammount of time 
                                          // it took to get the pulse back.
  Fdistance = Fdistance/58; // Dividing by 58 converts the raw time into centemeters.       
  return (int)Fdistance;    // Return an integer value of the distance.
}
  
void setup(){
  // Start serial port (used for testing).
	Serial.begin(9600);

  // Tell the Arduino what pins are used for the ultrasonic sensor. 
  pinMode(Echo, INPUT);    
  pinMode(Trig, OUTPUT);

  // initialize digital pin LED_BUILTIN (pin 13) as an output.
  pinMode(LED_BUILTIN, OUTPUT);

}
  
void loop() {
  distance = Distance_test(); // Call the distance measuring function.
  Serial.print("Distance= "); 
  Serial.println(distance);

  // See if the distance to the surface is less than or equal to the trigger
  // distance.  If so, light the LED, if not, turn the LED off.
  if (distance <= trigger_distance) {
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)  
  }
  else {
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  }

  // wait 1/2 of a second so as not to send massive amounts of data to the serial port.
  delay(500);
}
  • 2026 NRG Robotics Summer Camp Information
  • NRG Controls Resources
    • Bollard Bowing Example #1
    • Ultrasonic Example #1
  • Smart Robot Car Assembly Video

Code Red Byron Center High School Build Space

8500 Burlingame Ave SW
Byron Center, MI 49315
info@coderedrobotics.com
Tues & Thurs 7:00 - 9:00 PM
Contact Us Page

Donate to Code Red Robotics!

Click here to donate.

Latest News

  • Thank You for Joining Our Euchre Tournament!November 13, 2025 - 12:17 pm
  • Euchre Tournament at House Rules 🎉October 3, 2025 - 10:55 am
  • NRG 2024 Features Expansion and the Debut of Code Red’s Newest NRG GameApril 3, 2025 - 3:31 pm
  • FRC 2024 Season Recap: Amplifying our Team and CommunityApril 3, 2025 - 2:28 pm

Upcoming Events

No upcoming events

View Full Calendar…

© Copyright - Code Red Robotics
  • Link to Facebook
  • Link to X
  • Link to Youtube
  • Link to Instagram
  • Link to TikTok
  • Link to Mail
Scroll to top Scroll to top Scroll to top

This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies.

OK

Cookie and Privacy Settings



How we use cookies

We may request cookies to be set on your device. We use cookies to let us know when you visit our websites, how you interact with us, to enrich your user experience, and to customize your relationship with our website.

Click on the different category headings to find out more. You can also change some of your preferences. Note that blocking some types of cookies may impact your experience on our websites and the services we are able to offer.

Essential Website Cookies

These cookies are strictly necessary to provide you with services available through our website and to use some of its features.

Because these cookies are strictly necessary to deliver the website, refusing them will have impact how our site functions. You always can block or delete cookies by changing your browser settings and force blocking all cookies on this website. But this will always prompt you to accept/refuse cookies when revisiting our site.

We fully respect if you want to refuse cookies but to avoid asking you again and again kindly allow us to store a cookie for that. You are free to opt out any time or opt in for other cookies to get a better experience. If you refuse cookies we will remove all set cookies in our domain.

We provide you with a list of stored cookies on your computer in our domain so you can check what we stored. Due to security reasons we are not able to show or modify cookies from other domains. You can check these in your browser security settings.

Google Analytics Cookies

These cookies collect information that is used either in aggregate form to help us understand how our website is being used or how effective our marketing campaigns are, or to help us customize our website and application for you in order to enhance your experience.

If you do not want that we track your visit to our site you can disable tracking in your browser here:

Other external services

We also use different external services like Google Webfonts, Google Maps, and external Video providers. Since these providers may collect personal data like your IP address we allow you to block them here. Please be aware that this might heavily reduce the functionality and appearance of our site. Changes will take effect once you reload the page.

Google Webfont Settings:

Google Map Settings:

Google reCaptcha Settings:

Vimeo and Youtube video embeds:

Other cookies

The following cookies are also needed - You can choose if you want to allow them:

Privacy Policy

You can read about our cookies and privacy settings in detail on our Privacy Policy Page.

Privacy Policy
Accept settingsHide notification only