Outils pour utilisateurs

Outils du site


issue136:great_cow_basic

Ceci est une ancienne révision du document !


In FCM#129, I showed you how to use the analog-to-digital conversion (ADC) with a potentiometer, and how to control a LED with pulse width modulation (PWM) with the resulting value of the ADC. In this article, I will show how to use the ADC again, but this time we use a light dependent resistor (LDR) to gather information about the ambient brightness and how to communicate at distance.

Light dependent resistor A LDR, or photocell, is a light-controlled variable resistor. The resistance decreases with increasing light intensity. Hence you may use it to develop light-dependent applications to react to changes of the light intensity or the difference between light and dark conditions. In the dark, the LDR has a resistance of many megaohms; when the LDR is in the light, the resistance drops to a few hundred ohms. LDRs are available in different sizes (such as LED, e.g, 3 or 5 mm), and with different electrical resistances. Always review the datasheet, which contains the characteristics of specific LDRs.

LDR and the Analog Digital Conversion To use the ADC, we need to know how to connect a LDR to the microcontroller – which seems simple – like any other resistor. But here comes the tricky part: you need a voltage divider to connect it to get useful readings. Therefore, we need some theory before we can operate the LDR correctly. Using Ohms Law, the current which flows through a conductor between two points is directly proportional to the voltage across the two points. To calculate the voltage dividers values, you can use this equation. If we would use resistors with a fixed value of 100 Ohm for resistance, we could set Vin = 5 V, R1 as 100 Ohm and R2 as 100 Ohm and would have a fixed Vout of 2.5 Volts. Next, we assume R1 to be our LDR and assume further it's dark and the LDR has gained its highest resistance of 1 MOhm - then Vout would be 0.45 V. On the other hand, if we set the LDR as R2 and leave R1 at 100 Ohm the voltage would be 4.55 V. As you can see, knowing on which side the LDR resides is important to interpret the measured values. So, first you have to decide where the LDR resides in your circuit. After considering the placement of the LDR, the ADC has to be connected between R1 and R2. The schematic of the circuit looks a bit like a twisted L with a joint to the right side. In the diagram, the Vout would be the connection to the ADC channel on the microcontroller.

Over the air data transmission For the first experiments, I measure some changes of the LDR values by connecting the microcontroller and the LDR with the serial adapter and see how the values change by changing the light levels. Then, as a further development, it is interesting to check the light levels outside the house, to get an idea whether it is dark or not. To gather the data from the outside, a way to transmit the LDR values over air would be great. In the Arduino world, there are some technologies to achieve this: ZigBee, 2.4 GHz, Bluetooth, WLAN, LoRa to name a few. But, for simplicity, we use the cheapest method of wireless data transfer: a 433 MHz transmitter and receiver (for those not living in the EU, it would be 315 MHz). They are really inexpensive, a pair can be obtainable for a few Euros. On the vending platform of your choice, e. g. ebay, you can find them easily, as “Arduino 433 Mhz”, “Arduino 315Mhz” also the keyword “superregenerative” will help, the receiver is called “HX-RM-5V” and the transmitter solely is called “FS1000A”. Make sure you get only those with the plain transmitting / receiving circuits on them. Choose those with or without an antenna, if you do not have an antenna included, you can choose a thin wire. This setup can be unreliable because the data transfer is concurrent with other devices such as weather stations, remote power plugs, and the like. And read the article up to the conclusion if you are about to buy new ones.

For more important data, it is recommended to use other transfer methods. But, the simplicity with this hardware was intriguing: Just 3 Pins for a wireless data transfer, Vcc, GND and a DATA pin on both the transmitter and the receiver. Another point is the power consumption, transmitter and receiver draw only some mA of power which should give some neat wireless sensor nodes. With respect to the Arduino, there are some libraries to receive and send data over those modules. Multi-purpose: virtualwire, and its successor radiohead, or, for switching power plugs: rc-switch. Great Cow BASIC does not include a demonstration for this type of solution. Most of the demonstration code focuses on modem-like transmitter/receiver and the nrf24L01 (2.4 GHz) module support. So, how to create a simple solution to this problem? I asked myself why not use a plain serial connection?.. this might do the job with ease. After some research, it seems that this idea has been thought through and tested before, so I found proof in some Instructables projects (see links below for more information), and that my idea was feasible.

Transmission software Using the plain serial connection has another advantage. You do not need major changes to the setup from the early articles I have published in this series. The same program code to send data to the USB serial converter can be used, you would only interchange the connections between the USB serial converter and the transmitter. The following code might look familiar, because the ADC readings of a potentiometer and a LDR are similar, the only changes to the code was to rename the variable from POTI to LDR (code on next page, top right). Put an USB serial adapter with the RX-Line to the TX-Pin of the ATtiny13a, and start your terminal software to see what's going on there. At my site, during writing and testing the software, the surrounding light gives values around 66 - 71, after holding the camera light of my smartphone over the LDR the value increases to a maximum of 216.

Trial and Error After replacing the wires and heading over to air transmission, the results were very strange. This was the result for a quick installment: however, after soldering antennas (~ 17 cm) to both the transmitter and the receiver the results of the transmission improved. Achieving 40 cm distance with ‘over the air transmission’ and the quality was acceptable. Most reference articles I found suggest sending the data sometimes more than once. And, for the first test, shown below, this was proven by empirical testing (shown bottom right) The next test configuration was to set the Sender device 1m from the Receiver device. As it turned out, the characteristics of the receiver could not handle the constant data stream the software sent to it every second. The surrounding noise pollutes the data stream. To improve the reception reliability, the software has to be written with the characteristics of the hardware in mind, the simple 433 MHz set used is best used with short bursts of data and not a constant stream of data. Also noteworthy in this article: the Sender device is always on, even when no data is being sent, therefore the transmitter device always has power applied. In many other setups, I found that the transmitter device is switched off after transmit. Either by setting the microcontroller to a powersave status, or by switching the transmitter device over a transistor. So, it is getting a bit tricky to use this technique for a direct microcontroller to serial line connection.

Experimentation with Byte transmitted as preambles, as suggested in the research articles, were not of sufficient quality for the readability of the console output. The results were even the same if given a value as 0xFA or given a simple ‘dot’ as preamble. So, I added dots as pre- and postamble. The results on the console for the short range of 40cm to 1m looked promising and the data received was read as in the snippet below. After setting the Sender device to another room (~ 3m and a wall between), the results looked promising the first seconds, but after a while the data got unreadable. Even in the same room (~ 3m, but no walls) left unusable data on the console as in this other snippet (top right) So something was faulty in my code which gave poor results. After careful examination with the timings, the times the LDR values are sent and the last Byte value (Ser1Send 0b00000000) it seems that this last Byte value was too much. After deleting this line and recompiling it, the results (bottom right) were much better.

Breadboard circuitry To test the software, you might connect the Receiver directly to a USB to serial converter, connect 5V to Vcc, the RX to the DATA pin (only one of them, not both at the same time), and GND to GND. The transmitter device and the LDR should be connected to the ATtiny13a as seen below.

Conclusion The software serial solution from Great Cow BASIC did the job as expected. But, to get reliable results from the over-the-air connection was much more work than I had expected. Testing this little Transmitter outside was out of scope for this time due to my tight time budget. But, for a battery powered solution, I would implement some kind of power saving on the transmitter because sending data every 8 seconds without switching it off would soak up the energy of the most powerful battery soon. The compiled binary has 772 Bytes, so there is some space left to implement maybe a solution with a watchdog timer and a power saving routine can be added. On the hardware side, I maybe would change the receiver on the long run as there might be better solutions for this. Other receivers which were mentioned to work better and are not really more expensive than the used one are “RXB6”, “RXB8”, “RXB12”. If I would buy new hardware, I would maybe test another receiver as it seems that the used one is the one with the poorest reception you can get. The transmitter works fine and seems to have no better candidate.

For more reliable data transfers, it could be a good idea to implement another algorithm and receive the data first through another microcontroller, there are some suggestions in the references and elsewhere on the internet. Maybe change the transmitter and receiver side to a completely different solution would be another valid option. And, I would definitely add some kind of switching for the transmitter to power it off if no data is to be sent - otherwise your power plug cannot be switched - this project interferes with them. Essentially, it works. The Tiny13a can support LDR and data transmission over the air.

Sources If you want to download the sources instead of copy-pasting it, you can now check it out with git or an SVN client. Have a look at https://github.com/Anobium/Great-Cow-BASIC-Demonstration-Sources/tree/master/Publication%20Solutions/Full%20Circle for more information.

References Good introduction on voltage divider circuits https://learn.sparkfun.com/tutorials/voltage-dividers/ideal-voltage-divider Explanation about 433 / 315 Mhz receivers and transmitter, virtualwire library https://www.pjrc.com/teensy/td_libs_VirtualWire.html Simple transmit over 433 Mhz modules, using the USART - part 1 https://www.instructables.com/id/Wireless-Communication-Using-Cheap-433MHz-RF-Modul/ Simple transmit over 433 Mhz modules, using the USART - part 2 https://www.instructables.com/id/Wireless-Communication-Using-Cheap-433MHz-RF-Modul-1/ Tips for enhancement of the use of the 433 Mhz modules and a suggestion for a own transfer protocol http://www.romanblack.com/RF/cheapRFmodules.htm

Acknowledgement I wish to thank Evan Venn (Anobium) from the Great Cow BASIC Team for his insights and valuable hints.

issue136/great_cow_basic.1537276220.txt.gz · Dernière modification : 2018/09/18 15:10 de d52fr