Hi, Habr! Today is the continuation of an improvised cycle of detailed posts for beginners. In the
last post, we took a ready-made set of radio constructors with Arduino and an ultrasonic range finder in order not only to combine them, and to be able to tune the radio “with one hand,” but also to sort out “Arduinostroenii” from scratch. If you only have Arduino and the sensor itself, you can also try to collect something from this post, but, apparently, stop, after you set up a good ultrasonic line - this is also good. I wanted to go ahead and put the sensor on an expansion board to create a finished device. That's how it came out for me.

We stopped at the fact that if everything became more or less with the sensor itself and its pins (I hope, more than less), it is not clear to which pins at Arduino and why. Let's answer this question.
Pins on Arduino
Arduino Nano (chosen by chance, since it is small and suitable for prototyping moving and portable objects) is included in the package. There is a wonderful
layout of her pinot in (not the only one, but I like it the most).
')
As in the circuit diagram of the device, we so far will pay attention only to two parts of this scheme:

On the left we see pins 5 and 6. Look again at the schematic diagram of the device from the
first part of the post . From K4 and K5, the contact goes to pins D5 and D6. This means that the shield on which we are currently working transfers the voltage from these contacts to / from these pins: logical zero - 0 V; the logical unit is 5 V. Therefore, connecting something to these contacts, we will send \ receive a signal from these pins.
In principle, if, for example, we assemble a prototype not on a ready-made shield, but on a brainboard, you can connect the sensor directly to specific Arduino pins. In this case, the shield simply helps to conveniently and beautifully assemble the device on the board so that it does not crumble in the hands.
Why did we choose these two legs of Arduino? Two reasons:
1. It was they who “remained free” in our scheme.
2. They are the digital inputs / outputs that we can program to receive / send a signal.
Based on the last phrase, guess which pins of the sensor we connect to K4 and K5? That's right, digital input and output devices.
Mistress note:
Digital I / O or - more correctly - Arduino Nano’s digital lines are D0 - D13 pins; can be both digital and analog A0 - A5; only analog A6, A7.
Precisely because it is possible to hook up devices sending / receiving a signal to the digital inputs \ outputs of Arduino, we could add here any other sensor or component, for example: a sensor of temperature, humidity, color, sound, light, etc., or executive devices like relays.
This is important information for the future, since it is thanks to it that you can have an idea of ​​which components and how you can connect with your MC, creating your own prototype or device.
Power connection through Arduino
Ok, with two sensor pins sorted out. Now where to connect the sensor power - the two remaining pins plus and minus?
Arduino has several interchangeable power supply options. In our case, we can connect the finished device (radio) through the batteries (the battery compartment I have now soldered so as not to interfere), as well as via USB. And here, and there at the entrance, we get 5 volts - what we need.
However, to date, we have two pins of the sensor connected to the Arduino pins to receive / send a signal, and the pins of the sensor power supply are free. There is an exit! We can connect them to the pins located at the top of Arduino (see top of the pin layout). This is the part labeled ICSP. The dot marked pin # 1. Next, the pins are arranged in pairs:
1 and 2, where 2 is Plus, Vcc
3 and 4
5 and 6, where 6 is Minus, Grnd, Ground
Here all the same voltage of 5 volts.
Is it possible to connect a sensor here? Do you need, for example, resistors?
We look at his performance characteristics :
Power supply: 5 V
Power consumption in silence: 2mA
Consumption at work: 15mA
The correct answer is: you can. Resistors are not needed.
Important: if we mix up the plus and the minus, then the first time, we may not burn Arduino. Some components will heat up, some may start to smoke. But, if something really burns, then most likely it will be a sensor, since the power poles for it will be confused, and instead of 5 V it will have -5 V, that is, the sensor supply voltage will go beyond the permissible limit (5V per TTX).
Component assembly
This is what happens when we connect the Arduino and the sensor via an expansion card:

Only the shield, the Arduino and the sensor that we connected as shown above are shown here. We do not need all the other components yet, as we have not yet been informed about whether our new sensor is working. If you put Arduino in his place on the board, everything will be ready to test the sensor. Therefore, further with the "iron" everything, it is the turn of programming.
At this stage, if you do not use, like me, an expansion card from
this set , you can directly connect the sensor to the required Arduino pins and act further. Once again I repeat that, unfortunately, the author of the post is not responsible if you mix up something when you connect and burn Arduino or the sensor. So please be careful.
Programming: sensor test
Last time we learned how to load a sketch for the operation of the entire device and a driver library for the operation of its individual components. It seemed to us that IDE works approximately like iTunes, loading the program into an MC like music into a player. However, now we will learn an additional opportunity: it turns out that with the help of IDE you can directly ping a new sensor, checking its operation and setting it up. Here is how it is done:
Step 1: Download the
library for the sensor . If you don't know how to work with the library and the sketch, you can quickly learn it
here .
Step 2: Open the NoBlindDemo2 example in a text editor.
This is a .pde file —
Processin g files have this extension. This is the same sketch. Arduino understands .pde sketches in addition to its .ino files, because Arduino's environment is based on the Processing environment. You can read about other types of files for Arduino and how to correctly name them
here .

Reassign the used I / O lines, then save the sketch.
For TRIGGER_PIN, specify 5, for ECHO_PIN - 6.
Why did we do that? If the pin of the Trigger sensor receives a request from the MC, then let this request be sent to it by the 5th pin of Arduino. We decided so. As you remember, we have two free pins now - 5th and 6th, with this we have already figured out above. Accordingly, Echo sends data from the sensor to Arduino, and let it send them to the sixth pin.
Pay attention to the commands used in this sketch:

Notice that each line of the sketch ends with a semicolon. And also pay attention to the braces - they open and close at the right time. Also, note that any sketch is divided into two parts:
Void setup () {}
In this part you write what happens once at the beginning of the program. We have arduino connect to the serial port at the desired speed and write what went to measure.
Void loop () {}
Cycle. In this part, you write something that will be repeated until you turn off the power of the MK. In this case - measurement of the distance of the sensor every 50 ms.
Each part of the program fits between the corresponding {}
What is written after // the program ignores is the comments of the developer of the code to the one who will understand it later, that is, to us. As you can see, this code was written by a very responsible and accurate developer who commented everything.
Step 3: Download our new sketch to Arduino.
If you have never done this before, let me first tell you
how to do it.
Important: before any manipulations with Arduino, remove it from the board and de-energize it.
Step 4: Disconnect the Arduino from the USB. Put it on the board, connect the sensor. Reconnect the USB to the computer.
You will probably have to close and open the IDE again, because Now the device may not be defined in the Service - Serial port. Do not worry, this is a bug. Just disconnect and restart everything.
All the same, only now put Arduino on the board, too. Make sure that you are not confused + and -, otherwise the Earth will fly on the heavenly axis.Important: the author of the article does not bear any responsibility if you burn your Arduino. Remember, all the manipulations with your MC are done at your own peril and risk.
Last time we check that where:

Now you can connect USB.
Step 5: Launch the IDE. Make sure that Arduino "hangs" at its designated port:
Service → Serial port → the port is automatically registered here. Otherwise, the whole inscription is gray and you need to restart everything.
Step 6: Select Tools → Port Monitor.
Make sure that in the lower right corner is set the speed corresponding to our sketch: 115200 baud.
Select the speed corresponding to the one specified in the program. If you have already set this speed, then immediately after connecting Arduino with the sensor (after detecting it at one of the ports), the monitor will run to give you the Distance value, every 50 ms:

When turned on, the sensor lay on the table and looked at the ceiling, to which, as you can see, it was 180 cm.
Step 7: Move your hand away from the sensor. See how his testimony changes. In my case it was like this:

Congratulations to all of us: our sensor works! We just checked it with a test sketch that sends the results to the serial port.
Arduino radio on ultrasound control
All we have left now are two things:
1. Reassemble all remaining radio components back to shield.
2. Download to Arduino a sketch that combines radio and sensor. And for this we need to take a standard radio sketch and register something there.
But in general, it is already unrealistically cool, because we have just collected an ultrasonic ruler and now I know very well the distance at home from the table to the wall, window or cabinet and whatnot. Having played enough, I reassembled the radio with a new sensor and used for this:
Here are the libraries for the set itself.Sensor libraryHere is the sketchChange radio station.To change the radio station, you need to change the distance in increments of 5 cm.
Approaching the sensor - switching the station down the band,
distance from the sensor - switching the station up range.
For the step of changing the distance meets the constant DISTANCE_STEP.
Its value can be changed.
Exit station setup mode.After setting up the station, it is necessary not to change the distance to the sensor for 5 seconds,
tuning is completed with fixing the current station. During the "commit" is responsible
constant EXIT_TUNE_TIME. If necessary, its value can be changed.
That's all, I congratulate you on the assembly of a new device on the Arduino. You and I have learned a lot from these two posts:
1. Disassembled the characteristics of the new sensor and collected on it an excellent collection of links.
2. Learned to choose pins on the Arduino and on the expansion card under it.
3. Learned to navigate in charts and datasheets.
4. Learned to test the new sensor before introducing it into the project.
5. Collected and programmed the finished device.
And although (I hope) you feel freer with the “iron”, but it is important to note that I remember that so far we have tried to disassemble only one sketch - for testing the sensor. Therefore, if you need to write or change the sketch for a new device now, you will probably not be ready yet. Follow the new posts,
subscribe - I will definitely share with you what I know and can do in my future posts.
What would you use the sound range for? How can I change and add to my project? Welcome to the comments!
// ** PS In the comments I announce a contest for the best explanation of what baud is, why they have different speeds, and why this is important. There will be no prize, but I will be happy to try to understand exactly your explanation.