Saturday, September 12, 2009

Microcontrollers

Trying to learn microcontrollers. Last time I had a go at it was 2004, with microchip PICs. Time to take it up again, as opportunity arises. It's just simply something to do if you can, the benefits you will understand later.

There are some new developments since 2004 in the world of microcontrollers, mostly toward the more complex, such as Arduino, but otherwise, the simple and low power things have stayed mostly the same since, with PIC16F88 still being a top contender, with prices around $5. If anything, the rise of 10F200 like 6-pin PIC's priced at $0.44 each is new. Most of the (old) tutorials relate to PIC16F84/84A, and to PIC16F628. A lot of the world has moved on to AVR, partly because of partly because of C being more available, and possibly design reasons I will learn/understand later, but to start out, and to understand assembly programming, PIC's are still excellent. Knowing this stuff would eventually pay off, besides the hobby reasons - automating simple or mindnumbing tasks and collecting better information about one's environment is something to get better at. Home automation, even factory automation is on the table, if you can only get good at it. Most factory automation is still stuck on PLC's and hasn't move on to microcontrollers. PLC's are tried and true and tested and therefore well trusted, but they suffer on cost and power consumption.

I have some samples Microchip was kind to send for free way back in 2004 - PIC16F84/84A, PIC16F628, PIC16F88, PIC16F877, and some of the 18F series. Also from Dallas/Maxim some DS18B20, DS1631 and MAX232. My life went in strange directions and I haven't really had the stability to sit down and toy with these things.

Moreover there was not a really good goal to gravitate towards. Now I have certain things in mind, such as greenhouse/farmer irrigation/soil moisture/quality sensor networks. For this cost has to be rock bottom, and $5 for a microcontroller is too steep, literally, so eventually, the 16F88 is off the chart after the learning period, and 18F series are out of the question. The 10F 6 pin series are a prime target, but the resources are so limited on it, that the 12F 8 pin series, or the lowest cost 18 pin 16F628-like ones may have to be settled for.

Ideally I'd like to find a an accurate and economical way to measure moisture content, and base irrigation on that. I feel there is room for improvement that hasn't been exploited enough. Electronic sensor technology is very advanced, and lots of freshwater resources are wasted on improper irrigation. However measuring, or even definition of ground moisture is a fairly touchy subject. Most top of the line measuring instruments measure water tension, and cost $100+, and require a meter that an operator would walk around with. Wireless setups cost in the thousands and farmers are likely to skip on getting such systems, simply on initial cost reasons, even if the water savings/proper not under or over irigation savings are worth it overall.

Sensirion has some digital SHT11/71 humidity meters, that after a trial/experimenting, might be in the proper range and scale to measure soil moisture directly. However their $30 price is way off the chart. There are some chinese knockoffs for $9.99, but that is still too steep. In the end one might have to go with a Honeywell analog moisture sensor for around $1-$3, and use the microcontroller A/D convertor, with manual calibrations. That is if capacitive sensing works well, and is not in the wrong range. There is a possibility that soil shows a 100% relative humidity reading for moisture content from 30-90%, and then it suddenly drops to near 0%relative humidity in the air at some number such as 25% soil moisture content. In this case the resistive sensing through gypsum/plaster blocks might be the answer. However gypsum dissolves slowly over a year into the soil and has to constantly be replaced. Also it may be dependent on contact area/soil texture for appropriate response time. Measuring ambient conditions of the air in soil as in capacitive relative humidity sensors would do, would make the measurement independent of texture/compactness. Ideally one would like to grab an off the shelf cheap item that does not need much custom fiddling around such as manual recalibrations, and it should last 10 years, if possible, without having to replace it yearly, by design.

For a reasonable setup many sensors would be needed on the very cheap, discardable if a tractor/cow smashes them, and ideally connected wirelessly, to keep cost and management spaghetti messes down. Most recent wireless standards such as ZigBee are out of the question, on reasons of cost/power consumption. RS232 on each sensor/microcontroller for local debugging via hyperterminal is a must, on reasons of simplicity. There is nothing simpler than RS232. Only the parallel port used to be simpler as far as hobby programming goes, but unfortunately it is no longer available on computers, that come only with USB anymore, and everything else available as an adaptor. Unfortunately the LPT port is gone, even with USB/parallel adapters, because those only work with high latency, for printers only. Bitbanging the USB stack would be a nightmare from the programmer's perspective, especially if one only has a limited resource PIC10F200. Ideally new computers would still come with a 9 pin RS232 port that could be converted in the BIOS to a low latency 9-pin parallel port, 8 bits+1 GND. The 25 pin parallel port is truly a waste on manufacturing labor, but 9 pins is a tolerable balance point. The USB protocol has too much packet processing latency for microsecond reaction time parallel ports. At least we still have good old RS232 that works well through USB dongles, but accurate timing requirements still make it harder to use than parallel designs, where the clock can be slowed down/sped up at will. But alas, accurate timing is no longer available on nonrelatime operating systems, and even realtime ones only guarantee reaction times on the order of 1 ms, not microseconds. In some cases, such as printing to an LCD through a parallel port, such timing issues may not matter, and a thousand instructions might be sent with 10 us latency, then there could be a 250 ms gap as the OS requires the CPU for other tasks, then it's returned and the program continues with its 10 us routine. Debugging parallel port things with a multimeter, by setting the frequency very low, to 0.1Hz or so in the program is very simple. So bit banged RS232 it is, unless the ucontroller comes with built in RS232. Bitbanging is still much fun.
The question is a cheap RS232/wireless/RS232 bridge, on the order of a few dollars. The nature of wireless is in question. The cheapest RF link ones with adequate range are custom FM setups on 3-400 MHz, but only one or a few can be used, for reasons of interference. According to http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=128 Nordic has some very low power and adequate range chips, but all the other protocols, including wifi, bluetooth, etc. are very heavy and have extreme power consumption because of that. But even that chip functions in the 2.4 GHz band, making the range very short for a given power consumption, on the order of 10-50m. The lower the frequency, the deeper the penetration into such things as a forest full of moist leaves, and as long as the data throughput can be low, even long wave radio on kHz is more suited than 300 MHz setups.

I found interesting designs using laser pointers, that could be done very simply and on the cheap on dollar store items. http://www.geocities.com/siliconvalley/lakes/7156/laser.htm?200912 (yahoo killed geocities, but lot of webpages point to that old address, here is an exact copy http://www.qsl.net/n9zia/wireless/laser/laser.htm )
however the danger of staring into one, and having to maintain proper alignment may be too much an issue. There are high power infrared lasers in most CD/DVD burners, but some youtube videos show such high power cd burner LED's setting things on fire. Lower power infrared lasers might be safer on eye health, other than the heating effect that the eye is very sensitive to. Unfortunately the longer the range one wants to achieve, the higher the heating effect. Infrared beams are safer than visible because they don't directly excite human vision cells, but also not safer from heating effects, because they are invisible. (PS. 4.10.2010 -actually, the above thinking is incorrect, the blink reflex on visible lasers makes them safer than infrared, see wikipedia pages.) Infrared beams are visible with cheap usb webcams. One could use smaller power regular visible laser LED as a coherent partner beam for warning purposes. Such line of sight systems, if properly aligned, are safe from interference, because the light beams are localized, compared to most other wireless designs, where the signal spreads out in space on transmission, and not very directionally received.
Cheap long distance communication has always been done with light, with smoke signals or ship light morse code signaling. Without mobility requirements, with sensors that are meant to be stationary on the order of a year there should be a way to get line of sight, even if repeaters are needed around corners. The ultimate throughput and power consumption efficiencies lie with focused infrared/light beams. For high throughput through a thick forest one could erect tall repeater poles that extend above the canopy, and do line of sight light communication. Weather is an issue, but nothing beats not having to run a conduit cost-wise. Because of stand alone unhooked to the power grid nature, the poles would have to run off of solar cells/rechargeable batteries, and power efficiency would be crucial. But such setups should be able to cheaply bring even internet to even places like the jungles of Africa or mountaintops of Tibet.

The current off the shelf standard wireless setup with multipoint sensors is 802.11b/g/n wifi. For very few (6 or so) points Bluetooth can work over short distances through piconets, and I even have a Bluetooth to RS232 adapter, for $50, from a previous situation where the goal was to monitor a single sensor, a PH meter, and maybe control a valve based on the readings. This RS232 to Bluetooth wireless cost is obviously out of the question for farms, and most wifi to RS232 adapters run around $200+. TCP/IP is simply too complex and expensive to put on anything, compared to a simple serial protocol like RS232/485/1-wire/i2c. In the end one might go with the 433 Mhz links, and use it like a walkie talkie, with the microcontrollers turning them on and off, and broadcasting messages, and listening for ack to check for collisions and retransmissions. It can slow things down a lot, but monitoring soil moisture/temperature/pH needs to be done very infrequently.

pH would probably use colotrimetric dye/nafion membrane/led/optical receiver, that should be done for less than $5, but with a very limited pH range of 3 units (i.e. 4.5-7.5), or custom ranges based on indicator selection. The dye lifetime due to dye degradation might be an issue, but this should be still less expensive than standard glass bulb. Colorimetry could in theory do other things such as nitrate/ammonia/phosphate/potassium/calcium, though these are very difficult. Drying out of the membrane can render colorimetric packets useless, though a constant trickle of water from a bottle on a stick controlled by a microcontroller interfaced to a humidity meter near the other sensors can deal with this. Such sensor sticks stuck into the ground at 10 meters (greenhouse)- 50 meters (open field) intervals measuring soil moisture/temperature, wirelessly connected to irrigation solenoids and automatically calling for water as needed, plus showing a record of measurements on a computer log, could provide a farmer with better information and water/crop resource management. Such sticks could also be beacons for automated farm robots instead of GPS signals, to weed/harvest crops. Solar power or in dark areas such as mushroom growing, sacrificial iron corrosion battery based ultra low power microcontrollers might be doable. Iron is a plant fertilizer. Aluminum and magnesium might be more expensive and harder to corrode accurately.

In a greenhouse atmospheric CO2 levels may be a limiting nutrient under intense light. Replenishing it in a controlled way might increase crop yields tremendously. Unfortunately internal combustion engines nor heaters capture liquid CO2, because of the energy loss that would entail. One would probably need a fuel cell where oxygen is ionized and travels to the other side in a pure form, separated from nitrogen, and generates pure CO2+water, and the two could be liquefied and shipped as carbon fertilizer for a greenhouse. There is talk about carbon sequestration from power plants, and sticking it underground, but if storage of it could be done efficiently, and shipped around in bottles like propane is, or even as dryice in super insulated containers, making food out of it, and increased healthy human biomass, that would be a better idea.

So much of the world's happiness is dependent on proper nutrition, and historically, booming societies always formed near areas where plenty irrigation was available, with large food production surplus per capita, allowing the farmers support of specialization in society: priests, potters, blacksmiths, musicians. Usually such societies collapsed internally by overgrowing their new extended limits, so even plenty of food, not enough food is still eventually a problem, and staying within controllable limits populationwise is the only sustainable solution. World population boomed during the last century, mostly due to Haber-Bosch ammonia fertilizer derived from fossil fuels. As fossil fuels run out, agriculture has to be improved to maintain current population levels. Biotech is improving crop yields through genetic engineering, just in time, but something as simple as proper irrigation management can provide a great kick at the bottom line.

The current soil humidity / agricultural wireless sensor market is a mess, on the very expensive side as far as farmers are concerned. Farmers work extremely long hours, for very little pay, and support families on as little as $8000/year, which is very low, even if housing costs or other living expenses are less than for city dwellers. Farmers will spend $50K on a tractor, or more land, as soon as they can afford it, because that is directly and visibly related to productivity. Equipment directly eases their physical labor, and the muscular pain they can feel when they sit down. When to irrigate/fertilize, and how much, is a much more subtle topic, and usually winged, based on feel, or simple trial and error experiment over the years, because even the mistakes of a few kilos of excess fertilizer or hectos of water is an order of magnitude cheaper than a lab test, and even then, lab numbers don't directly correlate to crop yields, the experience of fertilizer/water vs. lab numbers, then lab numbers vs. crop yield has to be deducted from them. Economical and accurate measurement is difficult, but that's the only true way to do it efficiently, via measurement and numbers. Here's a quote from William Thompson (Lord Kelvin):

"In physical science, the first essential step in the direction of learning any subject is to find principles of numerical reckoning and practicable methods for measuring some quality connected with it. I often say that when you can measure what you are speaking about, and express it in numbers, you know something about it; but when you cannot measure it, when you cannot express it in numbers, your knowledge is of a meagre and unsatisfactory kind; it may be the beginning of knowledge, but you have scarcely in your thoughts advanced to the state of science, whatever the matter may be."

As long as we are on quotes from Kelvin, and on measurement, here's another one:

"You, in this country, are subjected to the British insularity in weights and measures; you use the foot, inch and yard. I am obliged to use that system, but must apologize to you for doing so, because it is so inconvenient, and I hope Americans will do everything in their power to introduce the French metrical system. ... I look upon our English system as a wickedly, brain destroying system of bondage under which we suffer. The reason why we continue to use it, is the imaginary difficulty of making a change, and nothing else; but I do not think in America that any such difficulty should stand in the way of adopting so splendidly useful a reform."

No comments:

Post a Comment