Thursday, December 13, 2018

ESP8266 / DS18B20 Temperature; Ajax Web Updates

I wanted to monitor temperature with a ESP8266, and display the results on a web page. I grabbed a DS18B20 temperature sensor, and created a web server to display the results. I decided to use Ajax, so that the results would autoupdate without having to reload the page. The question came up, what IP address did the ESP8266 grab, so I know where to point my browser. I had a I2C LCD, so I connected that to show the IP address. I probably should have assigned a static IP so that it never changes, but hey, now you will know how to use a LCD with a ESP8266.

I have the I2C LCD connected to the ESP8266 I2C pins, GPIO5 (SCL), and GPIO4 (SDA). The LoLin calls these D1 & D2 respectively. You can specify other pins in the Wire.begin() statement, but the library I'm using doesn't expose that command. My LCD does not run on 3.3v, so I have it connected to the USB Power pin, labeled VU on the LoLin.

The DS18B20 I connected to GPIO14 (D5), GND, and 3.3v, with a 4.7k Ohm resistor across the D5 and 3.3v.

When I put the IP address displayed on the LCD (or in serial monitor if you are not using a LCD) in my browser, I get the following display:

My next modification will be to email me when the temperature climbs above or drops below my preset "acceptable range", indicating a problem with my HVAC system.


Libraries:
OneWire and DallasTemperature found in Library Manager

Wednesday, December 5, 2018

Build a $200 Computer for Christmas

Do you want to learn Computers, Electronics, and Programming?

For the last 40+ years kids and adults alike have been building their own computers instead of buying prebuilt, preloaded systems. Times have changed, and so has hardware and software. Some of the coolest tech comes in kit form, so not a lot of building is required, but you can still put it together, and customize it the way you want.

For the budding programmer or electronics engineer, one of the least expensive ways to put together a full blown linux and IOT programming environment is with the Raspberry Pi. The new 3B+ is more powerful than previous versions of the $35 computer, and with onboard WiFi and Bluetooth, very flexible.

You can build a media server, a VPN device, firewall / router, security system, phone system, automated home control, and much more. Learn to program in Python, or even C/C++. Build web based apps with PHP and MySQL. The I/O pins allow you to read sensors and control relays and LED's. Your imagination is the limit! See Build a Home Automation System!

Let's see if we can build a complete computer system for $200 or less!

Raspberry Pi 3B+
The heart of the system, this single board computer includes the processor, ram, drive (sd card), video and audio, ethernet, WiFi and Bluetooth. A complete starter kit with power supply, case, sd card, HDMI cable, etc.
Costs about $85

Raspberry Pi 3 B+ (B Plus) with 1.4 GHz 64-bit Quad-Core Processor
1 GB RAM
Dual Band 2.4GHz and 5GHz IEEE 802.11.b/g/n/ac Wireless LAN
10/100 Ethernet
32 GB Samsung EVO+ Micro SD Card (Class 10) Pre-loaded with NOOBS
USB MicroSD Card Reader
CanaKit 2.5A USB Power Supply with Micro USB Cable and Noise Filter
Premium Raspberry Pi 3 B+ Case and more


Wireless Keyboard / Mouse Combo
This wireless keyboard and mouse combo makes it easy to use your new Raspberry Pi without wire clutter, and allows some distance between the units.
Costs about $40

Comfortable palmrest
Single usb wireless receiver
Over 2 year battery life








HDMI Monitor
You can use your existing HDMI enabled TV, or you can get this 20" LED HDMI monitor with built in speakers. This monitor gets the audio over the HDMI cable, so no external speaker / wire clutter. VESA wall mount ready. 2x HDMI ports, so another HDMI device can also be connected.
Costs about $70











Sensor Kit
For the electronics experimenter, add an optional project pack that includes 38 I/O gadgets, including temperature, relay, joystick, sound, and much more, with convenient solderless breadboard and Raspberry Pi I/O connector. Costs about $45

Tuesday, November 27, 2018

Python: Floating Point vs. Decimal

In our Arduino projects (programmed in C/C++), we often use a Raspberry Pi for collecting data, and the Python programming language.

Math with floating point is inherently risky, because a decimal number does not store exactly in binary.

in python,
1.1+2.2 = 3.3000000000000003

not the 3.3 you might expect.

we can force the result we are looking for by importing the Decimal module, and setting the desired precision (2 in this example):

>>>from decimal import *
>>> getcontext().prec = 2
>>> Decimal(1.1)+Decimal(2.2)
Decimal('3.3')

Arduino doesn't have a Decimal function, it uses floating point, but you can work around this by using Fixed Point Arithmetic

Friday, November 16, 2018

Why can't I upload to my Arduino Nano?

For all of you out there with Chinese NANO clones, or older Arduino NANO's, there has been a recent change. The upload baud rate has been bumped up from 57600 to 115200, and that causes a upload error.

avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0xf6
The solution is at hand!

Go to

Tools > Processor > Atmega328P (Old Bootloader)

and your upload woes should be solved!

Wednesday, September 26, 2018

Electrolysis Issues with Soil Moisture Sensor

One problem with testing for moisture in soil or for free water is electrolysis of the electrodes. As material is removed from one electrode and deposited on the other (a byproduct of using a dc current), the resistance changes, and the values coming from the sensor start to drift.

One way to solve this is to flip the polarity of the current each time you do a read. The following code will change the polarity of the pins (and the equation evaluation) each time through the loop.

This circuit will print the word "Triggered" if it senses moisture (even skin moisture) and light up the onboard LED. You could have additional sections of code for varying levels of moisture (very wet, wet, dry, very dry), based on the analog results.

Wiring:

Probe pin 1 - A0 //analog input
Probe pin 2 - A5 //digital output
2M ohm resistor between A0 (analog input) and A4 (digital output)

Code:

int state=0;
int aValue=0;


void setup() {
  // put your setup code here, to run once:
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);

  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:

  if (state==0){
    digitalWrite(A4, HIGH);
    digitalWrite(A5, LOW);
    aValue=analogRead(A0);
    Serial.println(aValue);
    if (aValue<200){
    Serial.println("Triggered");
      digitalWrite(13, HIGH);
    } else {
      digitalWrite(13, LOW);
    }
 
    state=1;
    delay(500);
  } else if (state==1){
    digitalWrite(A4, LOW);
    digitalWrite(A5, HIGH);
    aValue=analogRead(A0);
    Serial.println(aValue);
    if (aValue>800){
      Serial.println("Triggered");
      digitalWrite(13, HIGH);
    } else {
      digitalWrite(13, LOW);
    }
    state=0;
    delay(500);
  }


}

Thursday, September 20, 2018

Long Distance Serial with RS-422

PC's used to have RS-232 ports to connect things like mice, modems, printers, etc. Then technology moved on to USB. RS-232 had a maximum distance of 50', and USB is 9' (USB 3.0) to 16' (USB 2.0). If you really need to send data a long distance, RS-422 might be the trick. It uses two pair of wires, and CAT 5 is just fine. You communicate up to 4900'. Arduino's and other microcontrollers use TTL logic (3v to 5v for a HIGH, 0v for a low), and need a converter to work with other signals. We are using a inexpensive TTL to RS-422 converter called a YL-128. This module uses a Maxim 490 chip.

By connecting a Arduino to a YL-128 on each end of the wire, you can communicate as if the two Arduino's were side by side and connected directly.

Connect one Arduino RX1 to the YL-128 RXD pin (RO), and the Arduino TX1 to the YL-128 TXD pin (DI). Connect a second Arduino and YL-128 on the other end of the cable in the same manner.

Connect the two YL-128 modules Y-A, Z-B, A-Y. and B-Z.



You can then load a serial test sketch like the one at https://www.hackster.io/harshmangukiya/serial-communication-between-two-arduino-boards-d423e9

Friday, August 31, 2018

Driving Relays with the ESP8266

The ESP8266 has a current limit of 12ma, so we have to take care when driving relays.

First I start with a 817C Optoisolator. The datasheet says says the IR LED drops 1.2v, so I plug 3.3v source voltage, 1.2v forward voltage, and 12ma forward current into the calculator at http://led.linear1.org/led.wiz and I get a 180 ohm resistor as the optimum value.

Now, according to the datasheet for the 817C, the Current transfer ratio at 12ma is around 120%, so I can only conduct around 14-15ma through the photo transistor, not nearly enough to trigger a relay.

Let's have the photo transistor trigger an additional transistor, one that can allow the full current needed by the coil of the relay. I have chosen a 2N5551, since the datasheet says it can handle 600ma and 5v.

Now the relay. I chose a HK19F 5v 125ohm relay which, according to Ohm's law and the datasheet, draws 40ma. It's a DPDT relay (most commercial modules use a SPDT), but the contacts are only rated for 1A or so.

Don't forget a flyback diode for that relay. I used a 1N4148, which, according to the datasheet, easily handles the current and voltage of the relay.

If you want, you could add a LED / 150 ohm resistor across the coil of the relay as a visual indicator.

Now a simple

digitalWrite(5, HIGH);

makes it all happen!

UPDATE: 3v MOSFET instead of optoisolator. Simpler and cheaper!



Thursday, August 30, 2018

Sync'ing ESP8266 GPIO Pins to Arduino Pin Numbers

Many ESP8266 12E Boards come with Arduino style pin numbers labeled on the boards, but you use the GPIO pin numbers in your code. If you include the following lines of code in your sketch before setup(), you can use the number silkscreened on your board (different boards may have different pin matchings).

const byte D0 = 16;
const byte D1 = 5;
const byte D2 = 4;
const byte D3 = 0;
const byte D4 = 2;
const byte D5 = 14;
const byte D6 = 12;
const byte D7 = 13;
const byte D8 = 15;
const byte D9 = 3;
const byte D10 = 1;

Now you can use something like

digitalWrite(D6, HIGH); //uses D6 on the ESP8266

in your sketch.

Of the above GPIO numbers, not all of them are easily available for use. Some are used for flash memory (GPIO 6 - 11), others are used for boot and flash purposes (GPIO 0, 2 & 15). GPIO 0-15 all have a built-in pull-up resistor (use INPUT_PULLUP), just like in an Arduino, while GPIO16 has a built-in pull-down resistor. The safe ones are GPIO 1, 3-5, 12-14, and 16. All GPIO pins can source about 12ma, and sink close to 20ma.

The const keyword stands for constant. It is a variable qualifier that modifies the behavior of the variable, making a variable "read-only". This means that the variable can be used just as any other variable of its type, but its value cannot be changed. You will get a compiler error if you try to assign a value to a const variable (which is a good thing). Unlike #define, it types the "variable" instead of making the compiler guess. For these reasons it's preferable to use const instead of #define (https://www.arduino.cc/reference/en/language/structure/further-syntax/define/).

A byte stores an 8-bit unsigned number, from 0 to 255. This takes less space than an int, which takes up two bytes. You may see some code samples use uint8_t instead of byte. It means the same thing.

Further reading:

https://tttapa.github.io/ESP8266/Chap01%20-%20ESP8266.html

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/powering-the-esp-12e-nodemcu-development-board/

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-esp8266-lolin-nodemcu-getting-started/

http://esp8266.github.io/Arduino/versions/2.0.0/doc/reference.html





Thursday, July 5, 2018

ESP8266 Deep Sleep Mode

My Adafruit HUZZAH Feather ESP8266 consumes 0.080 amps (80 ma) in normal operation. With one wire and a single line of code, I can drop that to about 0.010 amps (10 ma), according to my USB power monitor.

I connected a wire from GPIO16 (pin 16) to RESET (RST).

I'm using the Arduino IDE to program my ESP8266, so at the end of my loop, I insert

void loop() {
    // other code in my sketch

    ESP.deepSleep(60e6);
}

Which puts the ESP8266 to sleep for 60 seconds, and then reboots, running through my code once more before going back to sleep. This greatly extends my battery life. You can change the 60 to whatever number of seconds you want the unit to sleep.

With a Adafruit TPL5110 Low Power Timer you can drop the power consumption of nearly any microcontroller or circuit to 20ua, for about $5.

Wednesday, June 27, 2018

ESP8266 Breakout Board

The bare ESP8266 chip is cheap, but has non standard pin spacing, and isn't easy to work with. There is a "Bread Board Friendly" breakout for it, but it has a couple of quirks.

1. It's wide enough that it takes the outer rows of pins on a standard solderless breadboard, so you need to connect your wires underneath, then plug the breakout board on top. I'll post a picture of this shortly.

2. Most descriptions for the board claim one of the included 10k ohm resistors (the right one) connect to GPIO2 (which would be wrong) when it's actually connected to GPIO15 (which is correct).

3. The middle "0" ohm resistor is actually a jumper, and needs to be removed if attaching a 3.3v regulator on the back side.

3. I recommend a external 3.3v supply of 600ma or more. As mentioned above, a 3.3v regulator can be attached to the back side, but ....

A number of resources claim the regulator is a LM31117-3.3v 800ma (and some adapter board dealers even include one). That part will not work, as pins 2 & 3 are reversed. You need a AP7215 (AP7215-33YG-13) 600ma 3.3v SOT-89 regulator. I do wish input and output capacitors (1uf is fine) would have been preinstalled on the board, or at least pads for them. Place one between pin 2 and gnd, and the other between pin 3 and gnd. Alternatively, you can use a external 3.3v regulator or module, as I am doing.



4. There are still a few more resistors you will have to add:
    GPIO0 to VCC with a 10k ohm
    RST to VCC with a 10k ohm
    RST to Gnd through a momentary (reset button)
    GPIO0 to Gnd through a momentary (program button) and a 470 ohm in series.



5. You will need a ttl serial programmer.

The good news is that it's very easy to solder the ESP8266 to the adapter if you have a fine point soldering iron.

Additional references:

https://tttapa.github.io/ESP8266/Chap02%20-%20Hardware.html

https://www.esp8266.com/viewtopic.php?f=13&t=6505



Tuesday, June 26, 2018

Coulomb Counting and Amp Hours

Do you need to know how much Energy a device is consuming? How about producing? Maybe you want to keep track of how much juice is left in a battery pack? Now you can know!

Cliff Note version:
Take a Amp reading every second (Coulombs). Add those readings together (running total). Divide total by 3600 gives you Amp Hours. Multiply by the Volts to get Watt Hours.

Building battery "Fuel Gauges" is simple when you use Coulomb counting. A Coulomb is equal to 1A * 1s, so if your current sensor records a 5A draw this second, that's 5 Coulombs. If draw is steady for a minute, that would be 300 Coulombs (5 * 60), and if for an hour, 18000 Coulombs (5 * 60 * 60). 18000 / 60 / 60 = 5 Ah
If over two hours, you count 36000 Coulombs, 36000 / 60 / 60 = 10 Ah. So take the total Coulombs you have counted for whatever period of time period you choose, and divide by 3600 (60 * 60), that gives you your Ah charge (or discharge).

The following code simulates a current sensor reading as a fixed rate (0.5A). It prints the Coulomb totals every second, and the running Ah total.

Code Link

Stay tuned as we combine this with a current sensor and a voltage divider to get Amps, Volts, Ah, and Wh readings.

ACS715 Current Sensor Project

ACS712 Current Sensor Project

Current sensing with a shunt

Wednesday, June 20, 2018

The ESP8266 doesn't have EEPROM?

Unlike the Arduino AVR based controllers, the ESP8266 does not have a dedicated EEPROM. We can however use a bit of flash memory as a emulated EEPROM, and we can use the Arduino EEPROM library to do it. Using the Arduino Put and Get code samples as they are doesn't work. However a simple

EEPROM.begin(1024); // both the put and get code

before the

EEPROM.put(address, variable);
or
EEPROM.get(address, variable);


and a

EEPROM.commit(); // only on the put code

after it makes everything work.

In the linked code samples below, I'm saving 2 strings of text (wifi ssid and password) to the EEPROM in the put code, and retrieving them in the get code. If you put a slide switch in your project, and run the two pieces of code depending on the switch position, you can reset the credentials from a built in web form. I will cover that ability soon.

ESP8266 EEPROM "PUT"

ESP8266 EEPROM "GET"

Fore more info, see:
http://esp8266.github.io/Arduino/versions/2.0.0/doc/libraries.html#eeprom

https://www.baldengineer.com/arduino-eeprom-stores-any-datatype.html

APRS Transmitter

The HX1 is a low-power amateur radio transmitter set to a fixed frequency of 144.390 MHz. In North America, this frequency is used for the Automatic Packet Reporting System, or APRS

APRS is a standard used by amateur-radio operators to broadcast live sensor data on a shared frequency. This data can be received by anyone with the proper equipment, and is aggregated on the internet through gateways run by members of the APRS community. APRS is commonly used to share real-time data including vehicle location (GPS), weather stations, high-altitude balloon tracking, remote sensor platforms, disaster relief coordination, and more. It’s effectively an open-source, open-data, community-run, free to use, IoT system with potentially worldwide coverage.

The HX1 is a 5V, low-power (300mW), narrowband (3kHz) VHF FM transmitter module. With a simple antenna, the range will be around 6 miles. Since this is a “bare” transmitter, the data input must be properly modulated and formatted. There are several Arduino software packages that can do this, see the hookup guide for information.

Note: Because it uses amateur radio frequencies, you will need an amateur radio license to use the HX1. It’s easy (and fun!) to get one.

Tuesday, June 5, 2018

Arduino connected Micro Epsilon Laser Micrometer

With the success of our Igaging / Mitutoyo and Harbor Freight digital measuring devices projects, now it's time to turn our attention to the Micro-Epsilon Laser Micrometer, optoCONTROL 2600.

There are several types of outputs, including analog, digital RS-232 and RS-422, and Binary or ASCII options. We chose BINARY on the RS-422 output (19200, 8N1), and connected a RS-422 to TTL converter to Arduino 2560 (originally a Arduino Nano, but the 2560 has 4 hardware UARTs).

The program we selected is "EDGELH" "Standard edge dark-bright".

Wiring is as follows:

Micro Epsilon DB25 (Manual is wrong):
Pin 8 (T+) to RS-422 A/R+
Pin 7 (R+) to RS-422 Y/T+
Pin 20 (R-) to RS-422 Z/T-
Pin 21 (T-) to RS-422 B/R-


RS422:


if you are using the blue YL-128
VCC to Arduino +5v
Gnd to Arduino Gnd
RSD to Arduino D19 (Rx1) // Serial1 Rx
TXD to Arduino D18 (Tx1) // Serial1 Tx

If you are using the orange 2578-TTL
VCC to Arduino +5v
Gnd to Arduino Gnd
TXD to Arduino D19 (Rx1) // Serial1 Rx
RXD to Arduino D18 (Tx1) // Serial1 Tx

I suggest a DB-25 M to RJ-45 F adapter, and make up (or cut down) a ethernet cable to connect to the screw terminals of the RS-422.



Code:
https://pastebin.com/EufVX0AM

With no object in the beam, take your measurement, and subtract that in the code from the measured value. the "zero" function on the unit display only affects the display and analog output, the digital output shows the measurement from the bottom of the beam. I'll be adding a "zero" button and save that value to EEPROM.


Friday, May 18, 2018

RPM into MPH? Building a Anemometer!

I'm building an anemometer to measure wind speed. I'm building the typical 3 cup style anemometer, and using a hall effect sensor (digital, non-latching) to trigger an arduino (or esp8266).

It's a open collector unit (output pulls low when triggered with a magnet), and therefore uses a 10k ohm pull up resistor, or use

pinMode(inPin, INPUT_PULLUP);

val = digitalRead(inPin); // read the input pin

and watch for a LOW (FALLING when using interrupts - https://playground.arduino.cc/Main/ReadingRPM)


Inside the unit, I want to pass a small magnet across the sensor. I could use 1 or more magnets, and adjust the code for the number of pulses (triggers) per revolution. Let's assume 1 magnet, so one pulse per revolution.

The math looks like this:

RPM = cycles per second (Hz) * 60 seconds per minute / # of magnets

(to count cycles per second or HZ - https://circuitdigest.com/microcontroller-projects/arduino-frequency-counter-circuit)

FPM = Diameter (inches) / 12 * 𝝅 * RPM

FPH = FPM * 60

MPH = FPH / 5280

So 3" / 12 * 3.14 * 100 RPM * 60  / 5280 = 7.14 MPH

Where:
RPM = Revolutions Per Minute
FPM = Feet Per Minute
FPH = Feet Per Hour
MPH = Miles Per Hour
𝝅 = 3.14 (pi)
Diameter = inches from center of shaft to center of cup * 2

More on this to come.

Optical Tach

Hall Effect Tach

Ultrasonic speed and direction, no moving parts
Related Posts Plugin for WordPress, Blogger...