Video Tutorial And Code For Object Avoiding Robot

Here is the code for object avoiding robot that uses one HC-SR04 Ultrasonic Object Detector.

int trigPin = 2;   
int echoPin = 3;   
long duration, inches;
void setup() {
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 pinMode(4,OUTPUT);
 pinMode(7,OUTPUT);
}
void loop()
{
 digitalWrite(trigPin, LOW);
 delayMicroseconds(5);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 pinMode(echoPin, INPUT);
 duration = pulseIn(echoPin, HIGH);
 inches = (duration/2) / 74;
 delay(100);
 if(inches<=10){
   analogWrite(6,127);
   analogWrite(5,127);
   digitalWrite(4,HIGH);
   digitalWrite(7,LOW);
 }
 if(inches>10){
   analogWrite(6,127);
   analogWrite(5,127);
   digitalWrite(4,HIGH);
   digitalWrite(7,HIGH);
 }
}

Learn how to set up a headless raspberry pi (needs to connected to the display for the one-time setup)
Check out other articles from learn2create.

Programming An Arduino With A Raspberry Pi

Program arduino using raspberry pi

The Raspberry Pi is an extremely useful mini computer. The one I have (The Raspberry Pi 3 Model B) has 4 USB ports, an ethernet port, built in wifi, and HDMI out. It may not be the fastest computer around, but it’s definitely small and versatile. Today, I’ll show you how to use a cheap Raspberry Pi as a simple desktop computer to program an Arduino for robotics and other tech projects!

 

The Parts I Used

Monitor/TV not shown

1 x DFRobot Romeo V1 (Arduino Uno with built in motor drivers)

1 x Raspberry Pi 3 Model B and 16gb micro SD with NOOBS os preloaded.

To learn how to setup a raspberry pi read this article – Setting up a raspberry pi for the first time.

1 x USB keyboard and mouse

1 x HDMI compatible monitor or tv

1 x HDMI Cable

1 x USB “A” To “B” Cable

1 x 5.25v 2.4amp micro USB power adapter

 

The Process

  1. Insert the micro SD card into it’s slot on the bottom of the Raspberry Pi. The metal on the card should be face the Raspberry Pi.

 

2. Insert the bluetooth receiver or USB cables for the keyboard and mouse.

 

3. Insert the HDMI cable into the Raspberry Pi and the monitor or tv.

[vc_row][td_block_ad_box spot_id=”custom_ad_1″ spot_title=”- Advertisement -“][/vc_row]

4. Plug in the power adapter and the chip will start up.

 

5. To program an Arduino with the Raspberry Pi, you will need to download the Arduino IDE so you can upload code to the Arduino. Read this article to learn in-depth how to install Arduino IDE on raspberry pi and connecting various arduino devices to it.

You'll need internet connection to download the IDE

 

6. Click the terminal button to open the terminal. This is where you will type in the commands for downloading and installing the Arduino IDE.

 

7. Type “sudo apt-get install arduino” and press enter. Eventually, you will need to learn the commands used in the Terminal.


[vc_row][td_block_ad_box spot_id=”custom_ad_1″ spot_title=”- Advertisement -“][/vc_row]
8. Type “y” and then hit enter. You have plenty of space on the Raspberry Pi right now so this isn’t something you need to worry about yet.

9. Once that finishes, you are ready to upload code to the Arduino! Connect the Arduino and the Raspberry Pi together with the USB “A” to “B” cable.

 

 

10. Open Menu -> Programming -> Arduino IDE

11. Once the program has opened, go to Tools -> Serial Port -> and select what is there. There should be only one option right now. If there are multiple options, trial and error is ok.

[vc_row][td_block_ad_box spot_id=”custom_ad_1″ spot_title=”- Advertisement -“][/vc_row]

12. Next, go to Tools -> Board -> Arduino Uno. The DFRobot Romeo board I used is based on the Arduino Uno.

 

13. You can now upload code to the Arduino and test it out! I used the “Blink” example that came with the Arduino IDE.

 

14. If the code compiles, uploads, and works correctly then you are finished!

 

You don’t need much to make awesome stuff. With a few essential components on hand, you can create an infinite number of devices for various uses.

Go build something cool!

To learn how to setup a raspberry pi without a monitor. Read our article – Setting up a headless raspberry pi.

Beginner Robotics: The Arduino Switch

Wouldn’t it be cool to have a house with futuristic technology, where everything is automated or computer controlled? Well, here’s a couple ways to rig up a little tech and coolness into your life! For both versions of this project, the same Arduino controlled light switch will be used, so let’s make that first.

 

Arduino Switch

The Parts

The robotics parts

DFRobot Romeo Microprocessor

HC-SR04 Ultrasonic Object Detector

2WD Mini Robot Platform Kit

Power Supply

Frame Material (I used lumber)

Lightswitch

Lamp

 

Connecting Everything Together

Connection diagram

The ultrasonic sensor’s trig pin is connected to pin 2 on the Romeo board, and the echo pin is connected to pin 3 on the board. Vcc goes to the 5v pin on the board, and Gnd goes to Gnd.

The motor is connected to M2, which means it is controlled by digital pins 6 and 7 on the board.

 

Building The Frame

You will need to build some kind of frame to mount everything on. I used lumber because it’s easy to work with and I had some lying around. It doesn’t have to be perfect unless you want a professional look, so just put something together and don’t worry.

 

As long as the center of the wheel is at the same height as the center of the switch, and the switch is between the wheel spokes, it will work fine.

 

Wiring The Switch

I used a regular lightswitch and just wired it into the hot wire of an old lamp. The hot wire is the narrower prong on the plug. Just cut the hot wire and strip the ends. Do not cut the neutral wire (The wide prong on the plug). On the switch I used, the wire coming from the wall connects to the bottom screw on the switch and the wire going to the lamp connects to the top screw.

DO NOT WIRE THE SWITCH WITH THE CORD PLUGGED INTO THE WALL OR ANY OTHER POWER SOURCE!!! I AM NOT RESPONSIBLE FOR YOU BEING AN IDIOT!!! THE AMOUNT OF ELECTRICITY COMING FROM A WALL OUTLET CAN KILL YOU!!!

[vc_row][td_block_ad_box spot_id=”custom_ad_1″ spot_title=”- Advertisement -“][/vc_row]

VERSION 1: The Touchless Switch

If you want to make the Touchless Switch, you are done building! This version is cool because all you have to do is wave your hand in front of the sensor to turn the switch on or off. Plus, think about all the germs not being spread! Time to upload the code and test it all!

Uploading The Code

Uploading code to the board is easy!

1. Download and install the Arduino IDE on your computer.

2. Plug in the board to the computer.

3. Select “Arduino Uno” from the Tools -> Board menu, if you are using the DFRobot Romeo board I recommended.

4. Copy and paste the code from this page into the IDE.

5. Click the upload button and you’re finished!

Here is the code:

int trigPin = 2;    
int echoPin = 3;    
long duration, inches;
int thing = 0;
void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(7, OUTPUT);
}
void loop()
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(5);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  inches = (duration/2) / 74; 
  delay(200);
  while(inches<=8){
    if(thing==0){
      thing=thing+1;
      analogWrite(6,127);
      digitalWrite(7,HIGH);
      delay(100);
      analogWrite(6,0);
      delay(100);   
      break;
    }
    if(thing==1){
      thing=thing-1;
      analogWrite(6,127);
      digitalWrite(7,LOW);
      delay(100);
      analogWrite(6,0);
      delay(100);
      break;
    }
  }
}

[vc_row][td_block_ad_box spot_id=”custom_ad_1″ spot_title=”- Advertisement -“][/vc_row]

VERSION 2: The Floating Magnet Switch

Another option for building the switch is to use magnets to control the switch. This is more for adding that “cool factor” to the project than function.

The Parts

A Shoe Box

Magnets

String or Wire

 

Building The Frame

Attach your magnets to your string or wire. I used wire because I didn’t have any string lying around. Poke a hole in the top and the bottom of the shoe box so you can run the strings or wires through. Tie them off on the outside of the box.

 

Make sure the magnets aren’t too far apart. If the magnets are too close, all you have to do is tie knots onto the string or wire on the inside of the box to pull the magnets further apart.

 

Once the magnets are attached to the box properly, stand the box up and hang the bottom magnet from the top one. Poke a hole in the side of the box at about the height of the top magnet. Run the wires for the ultrasonic object detector through this hole and then attach the sensor to the wires. Position the sensor so it is aiming at the bottom magnet.

Afterwards, follow the steps above to upload code to the board, but use this code instead:

int trigPin = 2;    
int echoPin = 3;    
long duration, inches;
int switchState = 0;
void setup() {
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(7, OUTPUT);
  pinMode(13,OUTPUT);
}
void loop() {
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    pinMode(echoPin, INPUT);
    duration = pulseIn(echoPin, HIGH);
    inches = (duration/2) / 74; 
    delay(200);
    while(switchState==0) {
      if(inches>5){
        break;
      }
      if(inches<=5) {
          switchState=1;
        analogWrite(6,127);
        digitalWrite(13,HIGH);
        digitalWrite(7,HIGH);
        delay(100);
        analogWrite(6,0);
        delay(100);   
        break;
      }
    }
    while(switchState==1) {
      if(inches<=5) {
        break;
      }
      if(inches>5) {
        switchState=0;
        analogWrite(6,127);
        digitalWrite(13,LOW);
        digitalWrite(7,LOW);
        delay(100);
        analogWrite(6,0);
        delay(100);
        break;
      }
    }
}

Like I said, this is a fairly easy project with a pretty cool payoff. Try it yourself and let me know what you think!

Beginner Robotics: The Avoider

The most crucial part of any mobile robot is the ability to avoid obstacles. What good is a robot that just runs into everything? Thankfully, object avoidance is an easy goal to achieve. With just a few parts and less than 100 lines of code, anyone can build a basic object avoiding robot!

The avoider

The Parts

All the robotics parts (Ignore how it is wired up in this picture!)

1 x DFRobot Romeo V1.3 All-In-One Microcontroller

2 x HC-SR04 Ultrasonic Object Detectors

1 x 2WD Mini Robot Frame

1 x 9V To Barrel Jack Connector

Cardboard

Electrical Tape

Mounting The Electronics

Making the cardboard sensor mount and attaching all the electronics

You’ll need to assemble the frame and figure out a way to mount the board, sensors, and battery. I used cardboard and electrical tape… So unless you’re going for a professional appearance, don’t worry about how it’s put together. However you want to do it is perfectly fine. It doesn’t have to be perfect! We’re just having fun, anyways!

[vc_row][vc_column][td_block_ad_box spot_id=”custom_ad_4″][/vc_column][/vc_row]

This is what it looked like once I attached everything

I used a small cardboard box that one of the sensors came in to raise the Romeo board above the wheels, then just electrical taped it to the frame. After that, I made a sensor mount using some taped together pieces of cardboard. The HC-SR04 sensors were also mounted with electrical tape, and I angled them slightly outwards to have more directional sensing. I ended up removing the rear swivel wheel and just letting the rear end slide around. The swivel wheel didn’t swivel very well, so it would get stuck facing one direction and make the robot turn unnecessarily.

 

Connecting Everything Together

Connection diagram

I wired the left sensor’s trigger pin to digital pin 2 on the Romeo, and the echo pin to digital pin 3. The right sensor’s trigger pin goes to digital pin 8, and the echo pin goes to digital pin 9. The Vcc wires coming from the sensors need to be spliced together into one wire and then that wire goes to 5v on the board. The Gnd wires from the sensors also need to be spliced together into one wire that connects to Gnd on the board. The motors just need to be hooked up to the green motor terminals on the Romeo. Left motor to M2 and right motor to M1.

Once, the robot has been assembled, the code can be uploaded and you can test it out!

[vc_row][vc_column][td_block_ad_box spot_id=”custom_ad_2″][/vc_column][/vc_row][vc_row][vc_column][td_block_ad_box spot_id=”custom_ad_3″][/vc_column][/vc_row]

Uploading The Code

Uploading code to the board is easy!

1. Download and install the Arduino IDE on your computer.

2. Plug in the board to the computer.

3. Select “Arduino Uno” from the Tools -> Board menu, if you are using the DFRobot Romeo board I recommended.

4. Copy and paste the code from this page into the IDE.

5. Click the upload button and you’re finished!

Here’s the code:

int trigPinLeft = 2;   
int echoPinLeft = 3;   
long durationLeft, cmLeft, inchesLeft;
int trigPinRight = 8;   
int echoPinRight = 9;   
long durationRight, cmRight, inchesRight;
void setup() {
 pinMode(trigPinLeft, OUTPUT);
 pinMode(echoPinLeft, INPUT);
 pinMode(trigPinRight, OUTPUT);
 pinMode(echoPinRight, INPUT);
}
void loop()
{
 digitalWrite(trigPinLeft, LOW);
 delayMicroseconds(5);
 digitalWrite(trigPinLeft, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPinLeft, LOW);
 pinMode(echoPinLeft, INPUT);
 durationLeft = pulseIn(echoPinLeft, HIGH);
 cmLeft = (durationLeft/2) / 29.1;
 inchesLeft = (durationLeft/2) / 74;
 delay(100);
 digitalWrite(trigPinRight, LOW);
 delayMicroseconds(5);
 digitalWrite(trigPinRight, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPinRight, LOW);
 pinMode(echoPinRight, INPUT);
 durationRight = pulseIn(echoPinRight, HIGH);
 cmRight = (durationRight/2) / 29.1;
 inchesRight = (durationRight/2) / 74;
 delay(100);
 if(inchesLeft<=10&&inchesRight<=10){
   analogWrite(6,127);
   analogWrite(5,127);
   digitalWrite(4,HIGH);
   digitalWrite(7,LOW);
   delay(1000);
 }
 if(inchesLeft>10&&inchesRight>10){
   analogWrite(6,127);
   analogWrite(5,127);
   digitalWrite(4,HIGH);
   digitalWrite(7,HIGH);
 }
 if(inchesLeft<=10&&inchesRight>10){
   analogWrite(6,127);
   analogWrite(5,127);
   digitalWrite(4,LOW);
   digitalWrite(7,HIGH);
 }
 if(inchesLeft>10&&inchesRight<=10){
   analogWrite(6,127);
   analogWrite(5,127);
   digitalWrite(4,HIGH);
   digitalWrite(7,LOW);
 }
}

Robots are easy so get the making started!