Go Down

Topic: Building a car (Read 260 times) previous topic - next topic

GreenYeti

In my current project I am trying to build a car. I have multiple questions regarding this project.

1. How should I mount my Arduino onto the car

2. What material should I mount the Arduino on

3. What type of motor would be best for making the wheels and how much current/voltage will it need



zwieblum

Use whatever you like ... and what you can work with. Plywood is a classic, easy to work with. You can build anything with thm, even the wheels. Or you can go to a to store and by a car of the dimension and design you like. That's probably the easiest way to go.

GreenYeti

Thanks for the answer. The car will use a ultrasonic distance sensor; the HC-Sr04. I was experimenting with it and have managed to make a relatively accurate distance sensor. However, i tried to make an led light up when the distance dropped below a certain amount, but it did not work. The distance measurements are still working properly, however the led instead of lighting up just has a very faint tiny glow which does not change no matter what I do.

This is my code:

const int trig = 8;
const int echo = 9;
const int led = 7;




void setup()
{
Serial.begin(9600);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);


}

void loop()
{
float duration,distance;

digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);

duration = pulseIn(echo, HIGH);

distance = (duration/2)* 0.0344;


if  (distance < 15)
{
 digitalWrite(led, HIGH);
  }


Serial.println(distance);



}


What am I doing wrong? 

zwieblum

your "if distance ..." is missing the else-Satement, where you change led from HIGH to low.


slipstick

Well for one thing you only ever switch the LED on. You don't switch it off again when the distance isn't <15.

But if it is very dim then what value resistor do you have in series with it? What Arduino are you using?

Steve

GreenYeti

Well for one thing you only ever switch the LED on. You don't switch it off again when the distance isn't <15.

But if it is very dim then what value resistor do you have in series with it? What Arduino are you using?

Steve
I am using a 220 ohm resistor with an arduino mega

zwieblum

And how is the LED wired?

GND -- 220 -- LED -- Arduino

or

+5V -- 220 -- LED -- Arduino

GreenYeti

And how is the LED wired?

GND -- 220 -- LED -- Arduino

or

+5V -- 220 -- LED -- Arduino
It is more like Arduino(digital pin)--220--LED--GND

And by the way can two brushed dc motors be controlled by an Arduino Mega along with a HC-Sr04 without an H-bridge

zwieblum

#8
May 18, 2019, 03:46 pm Last Edit: May 18, 2019, 03:47 pm by zwieblum
Ok, that should work, but maybe you have a LED from ancient times. Check the voltage on the pin when high and when low.

You can use small DC motors, but they have to be really small (<40mA current or > 125 Ohm), like pager motors, but you should use freerunning diodes (which only allows one way rotation) or live on the interestinmg side of eletronics (put the motor between 2 arduino pins, then you can play H-bridge).

BTW, have you modified your if-statement?

slipstick

And by the way can two brushed dc motors be controlled by an Arduino Mega along with a HC-Sr04 without an H-bridge
If you need to control both speed and direction of the motors then you need an H-bridge. If you only need to control speed then a couple of MOSFETs will do it. But you will need something.

Basically the motors and HC-SR04 can easily be CONTROLLED by a Mega. But motors must be POWERED separately. The Arduino pins can only safely provide tiny currents.

Steve

GreenYeti

If you need to control both speed and direction of the motors then you need an H-bridge. If you only need to control speed then a couple of MOSFETs will do it. But you will need something.

Basically the motors and HC-SR04 can easily be CONTROLLED by a Mega. But motors must be POWERED separately. The Arduino pins can only safely provide tiny currents.

Steve
]

In what way shall I power them?

GreenYeti

Ok apparently my led was the culprit, now once I swapped the led, the led emits a light of regular intensity. However, the led just stays alight for the entire time, instead of changing depending on the distance between it and the object.



GreenYeti

So I am trying to make the HC-Sr04 detect the distance, and while it is detecting a servo will spin from 0- 180 degrees and if the distance is below 20 cm the servo will stop moving, however I have the electronics working well but I am struggling with the code, can someone help

slipstick

Ok apparently my led was the culprit, now once I swapped the led, the led emits a light of regular intensity. However, the led just stays alight for the entire time, instead of changing depending on the distance between it and the object.
Did you read and understand posts #3 and #4? Have you changed your code to reflect the advice?

As for the servo, the latest code you've shown us doesn't even try to do anything with a servo so I think we need to see your current code.

Steve

Go Up