“The Destination has a lot of guises ...” or we automate the control of a bulb using CANNY 3 tiny and a photoresistor

In the last article on Habr, I told you about how I received the Canny3 tiny controller as a gift. We “blinked” the LED, pushed a button. On this, in principle, it was possible to finish the experiments.

However, for the last couple of weeks I have been listening to audio books about The Witcher and therefore have become much more serious about Destination . According to the information indicated on the manufacturer’s website, the CANNY family of controllers are primarily intended for the automation of various automobile systems and special equipment.

The problem is that no one will let me poke around in a real car with my “golden hands”, and in general I'm afraid of cars. Therefore, I had to buy a small car lamp and a photoresistor specially for this article. What you will not do so as not to anger the Destiny.

Today we will connect the controller and the car lamp to a 12V power supply, send a message to the COM port, automate the inclusion of the lamp depending on the light, and try the high-frequency PWM controller.

Do you also believe in the power of Destination? Then you are welcome under cat.





Part I: Introduction


Part I: Introduction
Part II: Components and Precautions
Part III: How to write data to the COM port
Part IV: Connect and program the autolamp and photoresistor
Part V: Trying to lower the lamp brightness using PWM
Part VI: Conclusion

At the very beginning of the article, I remind you I don’t have much experience in DIY electronics. Many years ago I dabbled with CraftDuino - the domestic Arduino clone. And apart from those "crafts" that he did for meDrzugrikthis is actually my experience in DIY electronics is limited.

Apparently, knowing my love for domestic developments, I decided to give Canny 3.

Even in the process of preparing the last article, I was surprised that the controller can be powered not only from 5V, but also from 12V, while the voltage at the outputs will be the same as on input, and the maximum current per channel is as much as 100mA. It seemed interesting to me, because my old CraftDuino could not.

By simple calculations using the formulaP=UII realized that the maximum power per controller output will be around 1.2 watts. It was decided to pick up a lamp with a power reserve, just in case. The choice fell on a 0.9 W LED bulb.

However, I still did not want to ingloriously burn my New Year's gift, and before connecting it, I decided to ask the developers if I understood everything correctly.
To my great surprise, they not only answered me, but also suggested that if you combine several outputs, then the power will increase proportionally. After that, it was decided to buy something more powerful than the lamp and the choice fell on the car fan, which is inserted into the cigarette lighter. However, in order not to make “vinaigrette” from the article, I decided to postpone manipulations with the automobile fan until the next time.


Part II: Equipment and Precautions


So, by the beginning of the experiments, I had the following:

  • Charging and launching device "Orion Vympel-55". I already had it and, fortunately, it can not only charge the batteries, but also be a power source in a wide range (including 12 V);



  • LED autolamp "Skyway H3. SH3-9SMD-5050 W. " As I understand it, the bulb can be used in various headlights, but I have nowhere to stick it, so I chose the power and ease of connection without soldering;



    By the way, returning to the power calculation formula, I decided to measure the currents and make sure that I fit into the permissible 100 mA. In theory, the current of the bulb should beI=P/U=0.9/12=0,075A.
    But judging by the measurement, it turned out a little less. Although maybe I just did something wrong.



  • Photoresistor FR-765. I chose him simply because there was nothing else in the store.



Well, of course I had: a controller, a breadboard, wires, crocodiles, etc.

In a previous article about Canny 3, I really praised the controller, but it turned out that there is one fly in the ointment. Last time I did not use the bundled cable bundle that came with the kit, but this time I could not do without it.

Since I use a soldering iron very poorly, I wanted to reduce its use to a vital minimum. Therefore, in the end, I collected all the circuits on detachable connections. I used special wires (dad-dad, mom-mom, mom-dad), breadboards and crocodile clips.

And if the developers of the controller are not to blame for the fact that in the process of stripping the cable I got a couple of cuts, then the moments in which my entire structure fell to the floor due to the fact that I hooked the "wooden" harness, partly on their conscience.

On the one hand, a tourniquet is a good thing. To install the controller inside the car, the cable harness I think is just right. The wires in it are stranded and if they are twisted well, then they completely fit into the connectors "mother" of my "breadboard" wires. On the other hand, how it is not convenient to do experiments with the cable with the breadboard, even a beautiful photo with the controller turned in full face was difficult to do because of the “oak wires”.

I said a little higher that I did not want to use a soldering iron, but at least once they would have to arm themselves.

In order to measure the signal from the photoresistor, we need to “insert” it into the analog-to-digital converter (ADC) of the controller. But the fact is that, unlike, for example, my old Craftduino. In the case of Canny, it turned out to be necessary to put a jumper.

In the beginning, I did not carefully read the documentation and thought that the 5 and 6 output of the controller are the outputs of A5 and A6. I even wrote a program and tried for a long time to take readings. Naturally, I did not succeed because by default, without preliminary preparation, channels No. 5 and 6 work as a normal discrete input.

After reading the documentation for the controller on a "fresh mind", I realized that I still have to solder. You have no idea how I was afraid to “screw up” everything.
The last time I tried to solder something in the circuit that I did for me Drzugrik, I managed to fill in all the capacitors with solder near the place of soldering.
This time the soldering process was simple. It was enough to very carefully put a drop of solder on the track.

What my solder jumper looks like can be seen in the photo below:



I have connected only the 6th output to channel A6, I did not dare to solder the fifth channel, so as not to accidentally fill both jumpers with solder.

Purely theoretically, it was possible to do without a soldering iron by connecting the hole and terminal No. 6 with a wire. But that would be terribly inconvenient.

The photo below shows the connection of pin No. 5 and ADC pin No. A5 with a dad-to-dad wire:



The last thing you should pay attention to before getting down to business.

Attention!Do not repeat one to one, what I will describe further on the text. Do not be lazy to replace the plug-in connections with soldering, pick up thicker wires, read the documentation. I am not an expert in these matters, so I put everything together "as best I could." I also advise you to carefully monitor the fire hazard, and not to leave equipment unattended. I managed to smell a few smoldering wires a couple of times. Well, even in the power supply there was protection against short circuit (I also arranged it). Therefore - please be careful.

Also, do not forget that in spite of the fact that the voltages and currents we have are small, still touching the live parts under voltage can be painful (especially if you poke them with a wounded part of the body) .


Part III: How to write data to COM port


In order to somehow calibrate our ambient light sensor, we need to understand what values ​​it sends to the controller depending on the light level. To do this, we need to output data from the ADC to the monitor. When working with Arduino and its clones to solve such problems, I used a COM port monitor.

In the case of Canny 3 tiny, the process of reading data from a virtual COM port is not much more complicated than for Arduino. But there is one key difference. The CannyLab development environment does not have its own COM port monitor.
I used Hterm because it is cross-platform, lightweight and does not require installation. But you can use any other.

I will say even more, nothing will stop us from using the Arduino IDE COM port monitor.

Everything worked out of the box on my Windows 10 system and I didn’t have to install drivers for the virtual COM port separately. But just in case, I’ll say that the driver seems to be bundled with the CannyLab development environment (it’s in the drv \ canny3tiny_vcp folder).

In order not to further explain how the chart blocks associated with sending data to USB virtual COM-port work, I will give a simple example.

Today, as a workout, we will send the message “HABR” using the controller. First you need to download the IDE CannyLab. It can easily be found on the official website of the developer.

By the way, I used CannyLab v 1.42, but I think in previous versions there will be no problems with the performance of diagrams.

The process of writing software to the new controller and the basic principles of working with Cannylab I outlined in Chapter 3 of the last article .

Create a new chart, select Canny 3 tiny in the dialog box. And we transfer blocks, constants and registers to the diagram, as in the screenshot below. Of course, it is not necessary to transfer text labels; nothing depends on them during the execution of the diagram.



If you don’t feel like moving blocks yourself, you can download the diagrams for this article using GitHub.

It should be noted that sending a message to the COM port works a bit strange and you have to write letters in a pair of characters in a mirror, but you can get used to it.

I will explain in parts.

In the "Parameter Setting" blockwe set the USB VCP driver settings. First, enable the transfer mode on the virtual COM port, and then set the length of the transmitted message. In our case - 6 characters including line feed.

In the "Message Text" block , we write the message text in constants (do not forget to switch to the "chr" mode) and transfer these constants to the corresponding driver registers. Once again I draw your attention to the fact that we write the message in the reverse order, both in the symbol pairs themselves and in the driver registers.

Last block "Message Transfer"Sends with a specified frequency our message to the COM port. For this we use a PWM block. Do not confuse it with the RF PWM, which we will use when connecting the lamp. In this case, the block generates half the time “0”, and the second half “1”. You can "play around" with the size of the period and fill to increase or decrease the frequency of sending messages.

Connecting a green LED to this PWM block is necessary in order to check the operation of the controller. If everything is in order, the LED will blink with the frequency of sending a message.

How to write a program to the controller, I already said in a previous article . However, over the past time I have learned another cool trick.

When we connect the controller to the computer, it enters the program recording mode. Previously, in order to go into runtime mode, I removed the controller from the computer and stuck USB charging into it.

It turned out that there is an easier way. After you have connected the controller to the USB port of the computer and clicked the “Connect” button in CannyLab , go to the “Device” menu and select “Run” .



The controller starts to execute the recorded program, receiving power from the USB port. Also after that the controller will become available in the monitor of the COM port. True, in order to switch back to the program recording mode, the controller will need to be removed and reinserted into the computer’s USB port.

And here is our “HABR” in the Hterm COM port monitor. Pay attention to the data transfer speed and other settings.



And this is the Arduino IDE COM port monitor:



The photo shows how the controller works after recording the diagram: The



red LED signals that power is being received via the computer’s USB port, and the green “blinks” to the beat of messages transmitted via the virtual USB COM port.


Part IV: We connect and program a car lamp and a photoresistor.


To begin, we will consider the simplest option. Just connect the lamp to terminal No. 1 of the controller, and the photoresistor to terminal No. 6 (ADC).

Everything is quite simple, but just in case, I drew a diagram:



But here it looks live:



“-” and “+” of the power source we connect to the corresponding terminals of Canny 3 tiny, pin No. 1 is connected to the lamp housing, and the “tail” of the lamp with a plus on the breadboard of the breadboard.

With a photoresistor, too, everything is simple. Plus, we bring the breadboard from the bus to the breadboard, and the minus goes to pin No. 6 of the controller, which we previously connected with the jumper to the ADC.

I already wrote above that my whole structure fell a couple of times from the table. Therefore, further on the photos there may be slight differences in terms of the colors of the conductors and the points of their entry into the breadboard.

Now we’ll write a program.



Consider the new day us blocks.

Let's start with the block “Getting the value from the ADC and converting it to suites” .
In this block, we translate the values ​​that the ADC (0 to 1023) can give us in illumination (lux).

We need to make one measurement of illumination when it is light and one measurement in darker conditions.

Accordingly, in the “value from” I recorded the minimum ADC reading “0”, and in the “result from” the zero value of the illumination level.

In the “value to” I recorded the ADC readings = 20, and the corresponding illumination equal to 300 lux, I entered in the “result to” .

I do not have a detailed description of the parameters of the photoresistor, but I suspect that it does not have a completely linear characteristic, but this is enough for us now.

Pre-check how the diagram is performed in the simulator. In more detail about work in the CannyLab simulator I wrote here .

Here is what I ended up with:



Imagine that the photoresistor passed a value of 10 to the ADC. The “Display (MAP)” blockthis is essentially linear scaling. Ten is exactly half of the scale of our scale, which means that the output will be half the range 0 - 300, namely 150. This is below the specified level of 300 lux. Therefore, one is sent to the channel register No. 1 (output No. 1) and the lamp turns on . But it will not turn on immediately, but only if it is dark for at least one second (1000 milliseconds).

To understand what message we will receive in the COM port, I switched the display of the values ​​in the simulator to hexadecimal format.



Consider the “VCP message setup register D1: D0 , the value 3130 comes into it. As we recall, this is a pair of characters, each part of which represents a hexadecimal character code. It turns out that 31h is the character code “1” in ASCII, 30 is the zero code in ASCII.

In the "VCP D3: D2 message installation register , we will accordingly have the characters" 0 "and" 5 ". We collect the values ​​of the pairs of characters in the order from D0 to D3 we get: "0", "1", "5", "0". So we got our 0150 lux.

But as they say "not a single simulator ...". Let's see what happens in practice.

Measurement under the light from the chandelier and natural light (combined lighting): The



readings of the factory light meter and our homemade one coincide almost perfectly.

Metering in daylight:



Extremely worse.

This is probably due to the non-linearity of the characteristics of the photoresistor, and also because we set the lower scaling limit to zero, but we will not complicate the diagram in order to increase the measurement accuracy; for our example, the main thing was to understand the general principle of obtaining illumination values. And increasing accuracy can be delayed until the implementation of these projects in the car or at home.

We check the operation of the circuit, close the sensor with a finger, and then remove the finger.



And now there’s an option when it’s just dark in the room:



The lamp is on. Everything works as it should.


Part V: Trying to lower the lamp brightness using PWM


Let's complicate the scheme a bit by changing the brightness of the lamp. To do this, we need a high-frequency PWM (RF PWM), in Canny 3 tiny it is implemented at outputs No. 3 and No. 4.
I decided to use output No. 4.

The connection scheme will remain practically unchanged. Simply, instead of output No. 1, we connect output No. 4 to the lamp.



This is how it looks in assembly. All the difference is that instead of a white wire, the frame is now blue.



The diagram, too, has not changed much.



I will not paint the diagram in detail. The main changes in the configuration of output 4, which we use for PWM. In this section, we must set the "Fill Setting Register"It takes values ​​from 2 to 255 microseconds, I decided not to risk it and chose 254. Note that it is installed for two channels at once (No. 3 and No. 4). Then we write “1” in the “PWM channel enable register No. 4” thereby including the PWM of our light bulb.

In the block “Turning on the lamp in PWM mode (with low brightness)”, new for us is only “Register for setting the filling channel No. 4, μs” We will write the value “10” in it, provided that the level of illumination above the photosensor was less than 250 lux, in within 1 second.

The photo below shows that the lamp turns on, but it shines dimly.



If you want the lamp to glow brighter, you only need to change the value of the constant “10” to any larger number. For example, you can take the number 127, in our case it is half the time it takes to fill the RF PWM period, which means about half the brightness of the lamp.



It should be noted that the RF PWM with this lamp works quite noticeably. Here are three photos: on the first, we set the value in the “Register for filling channel number 4” to 10 μs, on the second - 110 μs, on the right - 210 μs.



In principle, the autolamp from our example, as I understand it, is used in position signals and sometimes in fog lamps of cars.
I think that if we refine the scheme and the diagram, then it would be possible to implement the inclusion of backlighting in low light on older machines. True, you need to consider the effect of artificial lighting at night. So that on the roads, in the tunnels and from the headlights of oncoming cars, the lamp did not go out. Or it would be possible to control the lamp in the passenger compartment so that, for example, the lamps in the doors of the Zhiguli or Niva would not always light up when opened.


Part VI: Conclusion


In the process of writing this article, I experienced a strong sense of nostalgia.
While studying at the institute, my friend and classmate Seryoga had a chic blue VAZ 2107 brand sedan. What he just didn’t do with him: he’ll attach some kind of hose (to increase boost), install a fan in the passenger compartment so that he turns on by a separate button, then he will make the car start at the press of a button (only with the key turned ignition). In general, he just did not “tune” his “swallow”. Many years have passed since then, my friend moved to a more solid car, but I’m scared to imagine what he would do with his Zhiguli if he got this controller 10 years ago.

Thanks to this article, I also remembered my Destination. After all, I studied for many years as an automation engineer, but I did not work a day purely in my specialty.
Turns out it's so cool to dive into the DIY world. In the process of assembling the circuits more than once “stuck” turning the bulb and LED on / off with a button. It is a pleasant feeling when something works as intended. I also caught myself on the idea that programming a controller using diagrams is a more meditative task than writing code in the Arduino IDE. A completely different perception of the program.

While writing this article, I ordered myself a version of Canny 3 tiny without cable harness. Now I’ll have a little rest and write an article about installing this controller in a breadboard, and by itself we will analyze a few more examples of lighting automation with its help.

, , .

UPD:
, , :


  1. «, , – !» CANNY 3 tiny.
  2. « ...» (CANNY Arduino) Raspberry PI.
  3. « ?» bluetooth Xamarin (Android).

All Articles