Arduino blocks ultrasonic sensor

Hi,
we have been trying to make a car with Arduino blocks. We want to implement an ultrasonic sensor but it is not working. We are desperate because it has been like two weeks and we cannot find a solution. We hope that someone could help us.
Here there's an image of our coding blocks
image
Thanks so much!
-JJ

Hello jblancars

Welcome to the worldbest Arduino forum ever.

Do you can export this blocks to a real sketch to see what happens?

I really hate "code blocks" it's good for beginners to begin programming, but it becomes almost unreadable for larger codes.
Anyway first of all specify what "not working" means for you. No readings at all (always zero)? Reads wrong distances?
And about the hardware, are you sure you correctly connected the sensor? How is it connected, and powered? And are you sure the sensor (what exact model?) is not damaged? Could you provide here some kind of schematics of all the project connections?

PS: If your code is made with Tinkercad (is it? I hope so...) you can still switch to "Blocks+Text" and paste the code here, together with the answers to my previous questions...

1 Like

Hello, thank you so much both of you guys for your fast reply.

Okay, so we are doing a car and have connected two Arduino TCRT5000 sensors, which have worked well since last week when they started working weirdly. But we think that we are going to be able to fix them.
The major problem is the ultrasonic sensor which only works for one of our wheels.
We are pretty sure that everything is well connected and we have tried changing the motor of the wheels even the Arduino keystudio plate and nothing changed. So right now we are completely lost about why isn't working and we would appreciate so much your help. I'm going to send you the code that I have tried to download from our blocks.
Thank you so much for your time.
-jj`

#include <Servo.h>

#include "ABlocks_DHT.h"
#include <Wire.h>
#include "ABlocks_LiquidCrystal_I2C.h"
#include <ABLocks_TimerFreeTone.h>

double velocinicial;
double valorlinea;
double luzmedia;
double error;
double velocidadD;
double velocidadI;
double distancia;
String s_temperatura;
String s_humedad;
String s_comando;
Servo servo_9;
Servo servo_6;
DHT dhtA3(A3,DHT22);
LiquidCrystal_I2C lcd(0x27,16,2);

double fnc_ultrasonic_distance(int _t, int _e){
	unsigned long dur=0;
	digitalWrite(_t, LOW);
	delayMicroseconds(5);
	digitalWrite(_t, HIGH);
	delayMicroseconds(10);
	digitalWrite(_t, LOW);
	dur = pulseIn(_e, HIGH, 18000);
	if(dur==0)return 999.0;
	return (dur/57);
}

void clima() {
	s_temperatura = String("Temp: ")+String(dhtA3.readTemperature())+String(" C");
	s_humedad = String("Hume: ")+String(dhtA3.readHumidity())+String(" %");
	lcd.setCursor(0, 0);
	lcd.print(s_temperatura);
	lcd.setCursor(0, 1);
	lcd.print(s_humedad);
	delay(5000);
	lcd.clear();
	s_comando = String("");
}
void saluda() {
	TimerFreeTone(4,440,500);
	TimerFreeTone(4,440,500);
	TimerFreeTone(4,440,500);
	TimerFreeTone(4,349,350);
	TimerFreeTone(4,523,150);
	TimerFreeTone(4,440,500);
	TimerFreeTone(4,349,350);
	TimerFreeTone(4,523,150);
	TimerFreeTone(4,440,1000);
	TimerFreeTone(4,659,500);
	TimerFreeTone(4,659,500);
	TimerFreeTone(4,659,500);
	TimerFreeTone(4,698,350);
	TimerFreeTone(4,523,150);
	TimerFreeTone(A3,415,500);
	TimerFreeTone(4,349,350);
	TimerFreeTone(4,523,150);
	TimerFreeTone(4,440,1000);
	TimerFreeTone(4,880,500);
	TimerFreeTone(4,440,350);
	TimerFreeTone(4,440,150);
	TimerFreeTone(4,880,500);
	TimerFreeTone(4,830,250);
	TimerFreeTone(4,784,250);
	TimerFreeTone(4,740,125);
	TimerFreeTone(4,698,125);
	TimerFreeTone(4,740,250);
	delay(250);
	TimerFreeTone(4,455,250);
	TimerFreeTone(4,622,500);
	TimerFreeTone(4,587,250);
	TimerFreeTone(4,554,250);
	TimerFreeTone(4,523,125);
	TimerFreeTone(4,466,125);
	TimerFreeTone(4,523,250);
	delay(250);
	TimerFreeTone(4,349,125);
	TimerFreeTone(4,415,500);
	TimerFreeTone(4,349,375);
	TimerFreeTone(4,440,125);
	TimerFreeTone(4,523,500);
	TimerFreeTone(4,440,375);
	TimerFreeTone(4,523,125);
	TimerFreeTone(4,659,1000);
	TimerFreeTone(4,880,500);
	TimerFreeTone(4,440,350);
	TimerFreeTone(4,440,150);
	TimerFreeTone(4,880,500);
	TimerFreeTone(4,830,250);
	TimerFreeTone(4,784,250);
	TimerFreeTone(4,740,125);
	TimerFreeTone(4,698,125);
	TimerFreeTone(4,740,250);
	delay(250);
	TimerFreeTone(4,455,250);
	TimerFreeTone(4,622,500);
	TimerFreeTone(4,587,250);
	TimerFreeTone(4,544,250);
	TimerFreeTone(4,523,125);
	TimerFreeTone(4,466,125);
	TimerFreeTone(4,523,250);
	delay(250);
	TimerFreeTone(4,349,250);
	TimerFreeTone(4,415,500);
	TimerFreeTone(4,349,375);
	TimerFreeTone(4,523,125);
	TimerFreeTone(4,440,500);
	TimerFreeTone(4,349,375);
	TimerFreeTone(4,261,125);
	TimerFreeTone(4,440,1000);
	s_comando = String("");
}

void setup()
{
  	servo_9.attach(9);
	servo_6.attach(6);
	pinMode(12, OUTPUT);
	pinMode(11, INPUT);
	pinMode(A3, INPUT);
	pinMode(4, OUTPUT);
	pinMode(A3, OUTPUT);

	Serial.begin(9600);
	Serial.flush();
	while(Serial.available()>0)Serial.read();

	pinMode(A0, INPUT);
	dhtA3.begin();
lcd.begin();lcd.noCursor();lcd.backlight();

	velocinicial = 110;
	luzmedia = 360;

}


void loop()
{

  	valorlinea = analogRead(A0);
  	error = (((luzmedia - valorlinea)) / 15);
  	velocidadD = (velocinicial + error);
  	velocidadI = (((180 - velocinicial)) + error);
  	servo_9.write(velocidadI);
  	servo_6.write(velocidadD);
  	valorlinea = analogRead(A0);
  	error = (((luzmedia - valorlinea)) / 15);
  	velocidadD = (velocinicial + error);
  	velocidadI = (((180 - velocinicial)) + error);
  	servo_9.write(velocidadI);
  	servo_6.write(velocidadD);
  	distancia = fnc_ultrasonic_distance(12,11);
  	Serial.println(String("distancia")+String(distancia)+String("cm"));
  	delay(2000);

}

Please, edit your last post and enclose the whole code inside a single "code" tag. And make sure the code is properly indented (press IDE command Ctrl-T before posting). Thanks.

Like this? Thanks!

Yes, nice.

What exactly do you mean with "only works for one of our wheels"? Based on your code I can't see any relation between ultrasonic sensor readings and wheel commands, the "distancia" variable containing the ultrasonic measured distance is only printed over Serial and nothing more. what is exactly the problem you have with one of the moters?

Apart from the IR sensors, you need to specify what ultrasonic sensor you're using (I hope not the faulty SR-04, always consider using HY-SRF-05), how you connected it to Arduino, and, last but not least, what Arduino model we're talking about? Please note also "String" is a class you should generally try to avoid, because it can bring you to weird behaviours of the code, up to a complete lockup (if you don't know why, please read HERE).

PS: completely unrelated to your question, I see this:

 	servo_9.attach(9);
	servo_6.attach(6);
	pinMode(12, OUTPUT);
	pinMode(11, INPUT);
	pinMode(A3, INPUT); // <--- THIS
	pinMode(4, OUTPUT);
	pinMode(A3, OUTPUT); // <--- AND THIS

Why you put double A3 pin definition? You must need just one, but note as A3 is used for DHT you don't need to set it up with pinMode() at all.
Please note also you have a wrong "A3" parameter inside "void saluda()" (instead of 4):

...
	TimerFreeTone(4,523,150);
	TimerFreeTone(A3,415,500); // <--- THIS should be TimerFreeTone(4,415,500); ???
	TimerFreeTone(4,349,350);
...

Thanks for your fast reply and for the information,
Yes, it only works for one of our wheels, only one wheel turns on. The other just stays there, and we have already changed the motor and the wheel several times.
You said that you can't see any relation between our sensor and the wheels in the program. But this is because our Robotics teacher told us that we don't need to program the wheels with the sensor. He said that if only we connect the wheels and the sensor on the key studio plate it have to work with no need to program it.

The A3 thing we don't really know what it is, but we think it may be a pin that turns on when the ultrasonic sensor also turns on just to know when the sensor is working, but we don't really know.

We're really rookies and our teacher doesn't have any idea about programming. I don't know if it would be much more trouble for you to kind of program a small program for us including an ultrasonic sensor, two wheels, and TCRT5000 sensors.

Thanks so much for your replies.
-jj

Sorry to say, I suspect it's not his only deficit, and he's not the only one...

Sorry, I can't get what you and your teacher mean, I don't know what "key studio plate" is, and so on. Just to say, why install an ultrasonic sensor if you won't ever do anything with it? How can it "work" to do something?

Anyway, accepting the fact you don't need to do something with the ultrasonic sensor other than printing the distance, if I get into the code I see inside the loop() you have two times the exact same code/block:

void loop()
{

  	valorlinea = analogRead(A0);
  	error = (((luzmedia - valorlinea)) / 15);
  	velocidadD = (velocinicial + error);
  	velocidadI = (((180 - velocinicial)) + error);
  	servo_9.write(velocidadI);
  	servo_6.write(velocidadD);

  	valorlinea = analogRead(A0);
  	error = (((luzmedia - valorlinea)) / 15);
  	velocidadD = (velocinicial + error);
  	velocidadI = (((180 - velocinicial)) + error);
  	servo_9.write(velocidadI);
  	servo_6.write(velocidadD);

  	distancia = fnc_ultrasonic_distance(12,11);
  	Serial.println(String("distancia")+String(distancia)+String("cm"));
  	delay(2000);
}

There's no reason to do it, keep just one.

You means you want to do things understanding nothing about what you're using/building? Well, let me say it's not a good habit.

Anyway, "A#" pins are meant to be "Analog" ones, like "A0" that is used here to read the IR sensor. So in this case you have elsewhere defined a "DHT" sensor connected to A3 pin, but (other than wondering what is the purpose of a DHT sensor on a robotic car) I don't know why that sh*tty "Blocks" added the "pinMode" twice to the code side, with two differente modes (INPUT and OUTPUT). But if it was something kinda "automatic" and you really need a DHT, ok, just ignore it.
Let's get back to the (real) problem:

So, if the problem is a servo not moving, you need to now what are the values of "velocidadD" and "velocidadI" variables (I know a bit of spanish, so I suppose you mean the speed for right and left servos, the "I" is from "Isquierda" and "D" for "derecha"). But as you don't say what servo is not moving (and does the other is moving properly or what?) , I can just suggest you to add some debug serial prints to let you know the calculated values, like:

void loop()
{

  	valorlinea = analogRead(A0);
  	error = (((luzmedia - valorlinea)) / 15);
  	velocidadD = (velocinicial + error);
  	velocidadI = (((180 - velocinicial)) + error);
  	servo_9.write(velocidadI);
  	servo_6.write(velocidadD);

    Serial.print("velocidadI="); Serial.print(velocidadI);
    Serial.print(", velocidadD="); Serial.print(velocidadD);

  	distancia = fnc_ultrasonic_distance(12,11);
    Serial.print(", distancia="); Serial.print(distancia); Serial.println("cm");

  	delay(2000);

}

PS: sorry, I already said really hate blocks, so after removing the unnecessary duplicated blocks (from the first "Set valorlinea = Read Analog Pin A0" up to "Servo Pin 6 Degrees" included) if you need to use blocks just add two "Send" blocks to print such two variable values.

Said that, I suspect you need to double check the whole code and the algorythm used for the servos (example, I see on your codeThe "luzmedia" value never changes except for the initial value 360), and how you use the servo commands (servo.write requires a degrees value, not a speed).

You also need to tell us more about what you need the robot needs to do, and what is the whole hardware you're using: what exactly is the kind of your servo motors? What servo driver? You just said us you have on your car two IR sensors (obstacle detection, right?) but I can't see them both in your code: other than some unused functions (like "clima" and "saluda"), a reference to the DHT sensor you don't really use (it's used on "clima" but if you don't ever call that function...), I see I see on your code just a reading from the A0 pin (I suppose it's one of the two IR sensors), used to somehow get a "speed" value for each motor.

Sorry, everything is so confusing for me, we can't understand your project and purpose, and you can't learn anything doing such random things without any sense.

Okay, I am sorry if everything looks so confusing.
So, our program consists of a car that has to follow white lines. Okay so, what we want to do is with two TCRT5000 sensors in Spanish called "following lines" so to connect one with one motor of a wheel and the second sensor with the other motor of the other wheel. We want that the two sensors control the speed of the wheels. When one sensor stops detecting the white line we want it to decrease the speed of the wheel that it is working with, and the other sensor because it is detecting the white line we want it to maintain the speed of its wheel and try to get the car to the course of the line again. This could happen because of bends in the model of the road, so when a bend comes the idea is that the car just does what we have just texted you worked...
I know it is kind of confusing because the project is weird, but the main idea is to build a car and a model of a road and to program with it.

About the ultrasonic sensor what we want to do, is that on this road we find a wall in the middle of it, and the sensor detects the wall and makes stop the car.

About your program question, we didn't know anything about how to program the idea that we have just texted you. We loocked in Arduino blocks research programs and we copied two that looked similar to what we had to do. So that's the reason why the program looks weird because when we copied we didn't understand them, but we were like if it works for them let's just try the programs. Also to understand the code that you send us it is really puzzling for us because we are not used with this kind of coding.
Thanks again for your time and for just helping two guys that don't know anything about programming.
-jj

Ok, but a complete description of the project is always necessary to help us better understand what you're asking and trying to do. Always include a description and, if possible, a schematic and the whole code. In this case you have told us you have two TCRT5000 sensors, but you still miss to say what motors and motor drivers you are using (the best is to post a link to the exact item you have bought). For example, you are talking about "servo", when a "servo motor" isn't designed to be used with a wheel, you probably have a couple of standard DC motors, and controlled with a motor driver like one of the dual H-Bridge motor drivers with L298 chip.

So, if the usage of Blocks is not mandatory please throw it away and start a conventional coding with Arduino IDE: on one side you'll better learn what you are doing, and on the other we can help you better than with a bunch of screenshots (if you post a code we can copy and test it locally, with blocks we can't do it and force us to recreate the block codes, and the result will be you'll have less answers/help from us).
Secondly, always define your project evolution in steps, proceeding to the next when the previous il completed and working.

In this case the project is a classic "line follower car" (you can find tons of examples around the net, including tutorials and YouTube videos), where you later wish to add an ultrasonic obstacle detector.

So now keep the ultrasonic sensor topic away from the code and your mind, and tell us now what motors and motor diver you have to let us give you more information and help.

After doing that, you can concentrate on the primary goal (have a working "line following car"), trying to understand what you're doing, step by step. Start with some code you can find around (you could start from here for example), make some testing and post here the results.

Sorry, but you (and your teacher) can't think to be able to define and manage any project without any basic knowledge of Arduino, motors, and programming.

To give you an easy to understand image of your situation:
Your imagination of how much work and how much learning is nescessary to make your project work is more than you think.
The microcontroller-world is v superstandardised like USB-devices.

You have to take care of more details than just

"does the plug fit into the socket?"

here aresome pictures that iluustrate this

best regards Stefan

Okay, that sounds great.
So we have to hand this project in in less than two weeks. But I am fully committed to trying to learn Arduino IDE and do this project as fast as I can.

The products that we're using have not been bought from us but I'll show you which they are. The problem is that I cannot buy new ones or anything I have to stick with them.
Two motors for the wheels - https://acortar.link/v6yzC2
Two wheels - https://acortar.link/DRITkH

Moreover, we have that thing that I texted you calling it keystudio plate.
keystudio - https://acortar.link/POcsLx
also, we have another keystudio but I couldn't find it, it looks really like the one above but it is for connecting the wheels. If u want I can send a picture.
One ultrasonic sensor - https://acortar.link/Yoppb9

Le me say again you need some basic structured plan when designing a project, but those are things your teacher MUST tell you, even if he/she doesn't have any knowledge about Arduino! Everything looks strage to me, but, ok, let's go ahead...

For this kind of Arduino projects (not limited to that) you need to:

  1. make a description of what the project should do, e.g. its goals, the behaviour and response to inputs, etc.
  2. make a part list (Arduino, sensor, power supplies, cables, LEDs, buttons, etc.)
  3. make a diagram of the connections
  4. buy the hardware
  5. start connecting the items based on the above diagram
  6. start coding
  7. check the functionality, if not correct repeat from step 6 (if hardware change is needed, go back to #3)

You already made the #1, #2 (partially, if you miss the motor driver) and #4 (based on #2).
Don't start coding until all the steps from #1 to #5 have been completed (if you do so, you will just have a big confision in your mind, and need to review everything fomr #1 onwards, resulting in a big loss of time and neurons!)
Let's see the hardware you say you have (step #2):

Ok, good, this is basically an Arduino UNO with some extended headers to easily connect devices. You can consider it as an Arduino UNO because that's exactly identical.

Ok, they are standard DC motors, they aren't "servo" motors. It's not important for the scope know what kind of plastic wheels (besides, they're very common on this kind of projects).

For step #2 what you still miss is how you connect the motors to Arduino (forget about the name "keyestudio"). DC motors can't be connected directly, they need a "driver", a specific circuit to give the motors the needed current to let them rotate, based on your intended speed.
If you have it but just forgot to mention, please do it now, give us also this link to the exact motor driver.
Remember also that motors need a specific power source: the ones you likned here are 12V motors while Arduino requires just 5V, so you need to include two power sources. If Arduino can be powered from Vin pin up to 12V, I find hard for you to put 12V batteries over it. I hope the motors you have aren't the ones you linked, but some of the common 3-6V DC. Check this, as it's an essential part of circuit design!

Then, you need to do step #3, the connections diagram. Where is it? At least a draw over a paper sheet, but you better start using one of the free tools available, I suggest you to create an account on Tinkercad and create your diagram on the "Circuits" section of tinkercad. Once done, you can take a screenshot of the diagram and paste it here to show us.

But don't proceed to next steps after #5 unless you have cleared all the previous ones!

About the "driver" that you are metioning I don't really understand you. What we have done is to put a keystudio and above it this type of other key studio called here 'keystudio_motor' which can conect with the wheels. I'll instert you a picture. Also the link is - https://acortar.link/3RMIQa

the motors are these exact ones - https://acortar.link/Z8HbqV
and here it says that its voltage it is: 3.0~12.0V DC

These pictures in the bottom show how we have connected the wheels with the keystudio. I know that it looks like I mess and that we haven't done a thinkercard but we have these already done. Also, you can see that it has been already some time since we started this project but we have been so lost. Before this project we already did one traffic lights street and we have a little idea about cables and everything.

Thanks for your help,
-jj



What you don't understand? I asked you what motor driver you're using, and only now you cited such shield, made to connect over Arduino to control the motors ("shield" is the conventional name for boards that can be directly inserted over Arduino, vertically connecting to all of its pins). And it HAS the same L298 chip I told you before. And only now you also linked the exact motors you have, together with at least some pictures of your hardware.

Sorry, but if you don't give all the necessary information, even after I repeadetely asked it, it's not my fault.

Yeah, the pictures are better than nothing, but it's pretty hard to follow the cables around, you even soldered together Dupont cables (they are meant to be inserted into striplines), why you haven't used the empty perforated veroboard, left exactly to add components and connectors? I mean this area:
image

I hope everything is correct, I assume you have done the first steps above, right? So we can go ahead to implement this line following car.

First of all, I recommend you to have a look a THIS similar project I already suggested you (it has also a VIDEO).
If you now read it (please do it!), you'll find that this project is essentially the same as yours, except for the motor driver type: here is a separate board instead of your shield, but it's an L298 chip (called an "H bridge") like yours so its function is exactly the same.
You could/should start by copying the code shown, change the pin configuration according to your ones (for motor driver and sensors), and try it. With that simple code you could start better understanding the code, its purpose, and how can be used. If you are in doubt, ask us here!

Please, do it now and let me know if you start getting some results or not. In case, post the code you used (after changing the one you found on that page) and what is its behaviour, and we'll try to help you more consistently.

PS: Anyway, a very small note, its' just a matter of to be clear: please stop calling it "keystudio" (besides, it's "keyestudio" with a second "e") as it's the company brand, I mean, it's the manufacturer not the model. I hope you don't call all cars "Ford" and "Shell" instead of "gasoline". :wink: What you have is an "Arduino UNO compatible" (made by Keyestudio) and a "motor driver shield".

1 Like

First of all, I am sorry to answer that late, but the problem is that we only take this subject three times a week and they don't let us bring the projects home. So, I saw your text, but I only could watch videos this weekend about Arduino IDE for beginners.

we don't really understand what you mean about placing our sensor in these holes. However, if we think that the cables are well connected we will continue like this.
We also copied the code that you sent to me but the only problem is that even connecting Arduino UnO with the computer this happens.
image
So from now on we can not even try the program.
Thanks,
-jj

I have never mentioned the sensors, that area is just meant to host connectors (i.e. striplines where you'll insert Dupont cables or other connectors) and/or components (resistors, transistors, ICs, buttons. etc...) avoiding "floating" things like the ones we can see in your picture, and making the things cleaner. But of you're happy with that web, ok.

This is a problem that has nothing to do with everything we're talking about. That's a problem with your serial-USB connection. What version of the IDE you are using? It looks like it's a 2.0 (I really hate it, I'm still stuck with the legacy 1.8.* version, IMHO much more reliable).
And what is your OS?