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 ASCII on the RS-422 output (9600, 8N1), and connected a RS-422 to TTL converter to a Arduino Nano clone using SoftSerial. Wiring is as follows:

Micro Epsilon DB25:
Pin 8 (Tx-) to RS-422 R+ //inverted data
Pin 7 (Rx-) to RS-422 T-
Pin 20 (Rx+) to RS-422 T+
Pin 21 (Tx+) to RS-422 R- //inverted data
Pin 22 (gnd) to RS-422 gnd

RS422:
VCC to Arduino +5v
Gnd to Arduino Gnd
Tx to Arduino D4 (Rx) // now Serial1 Rx
Rx to Arduino D3 (Tx) // now Serial1 Tx

Follow along at the Arduino Forum as we attempt to decode the data from this gadget!

UPDATE: We have switched to a MEGA2560 with it's hardware UART's, instead of using the Nano and SoftSerial. This allows faster data acquisition.

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 = RPM * Diameter (inches) / 12 * 𝝅

FPH = FPM * 60

MPH = FPM / 5280

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.

Thursday, April 26, 2018

The Arduino 18650 Lithium Ion Battery Charger

The 18650 Lithium Ion Battery is becoming the go to battery for rechargeable projects, from microcontrollers to skateboards and ebikes.

We have been powering our Raspberry Pi, Arduino, and Ham Radio projects with them.

Although soldering the tabs on these batteries have been difficult (and damaging) in the past, there are now solderless interlocking kits that make them very easy to use.

It's extremely important to make sure these batteries are charged properly! Henry's Bench comes to the rescue with a Arduino controlled charger for these batteries. Just add a LCD screen to see voltage, amps, and cell temperatures.



Explanation of Lithium Ion charging profiles and dangers - http://batteryuniversity.com/learn/article/charging_lithium_ion_batteries

To learn more about building battery packs with the 18650, the master tome of all info is DIY Lithium Batteries (How to build your own battery packs), by Micah Toll (subscribe to his YouTube channel).





Tuesday, April 17, 2018

Adding a LCD to a ESP8266

Tutorials for adding a LCD to a Arduino are all over the net. It's pretty simple, and I2C makes the wiring even simpler, and libraries prevent complicating the code much. However, adding a LCD to a ESP8266 is not as common, and there is much misinformation posted about logic levels, and what not. So here is a working project using a I2C LCD, and a ESP8266. Wiring is very simple.

SCL to SCL
SDA to SDA
(these two pins are redirectable to another set in the Wire.begin command:

//Wire.begin(); default
Wire.begin(0, 2); // specified SDA, SCL pins

Gnd to Gnd
+5v to +5v (USB or Vin)

Yes, the LCD prefers 5v, and it won't affect the 3.3v logic of the ESP8266, as the ESP8266 I2C is in output, not input mode.

Some slower I2C devices won't work out of the box with the ESP8266 because of timing issues, and a stretch adjustment needs to be made. We found one I2C display that would not register on the I2C scanner sketch, but the rest did. If the scanner finds it when connected to a UNO, but not a ESP8266, place the following command after the Wire.begin line.

Wire.setClockStretchLimit(500L);

Make sure you run the I2C scanner first, to check what I2C address your display uses. See more in our Instructable at https://www.instructables.com/id/I2C-LCD-ESP8266/

If you don't have one already installed, you will need to install a I2C LCD Library. Those instructions are included in the Instructable.

Saturday, March 31, 2018

Controlling a ESP8266 with Alexa

Here is an easy project using a Echo Dot (Alexa) to control my ESP8266. I give it simple commands (Alexa, lights, off) and the command shows on the LCD screen attached to the ESP8266. Actual relays will be the next step to attach.

The project is based on the Adafruit tutorial at https://learn.adafruit.com/easy-alexa-or-echo-control-of-your-esp8266-huzzah/overview

with LCD and other modifications made by me. My code is found at https://pastebin.com/NjMcsTRh

Also see my ESP8266 LCD Project at https://www.instructables.com/id/I2C-LCD-ESP8266/



Related Posts Plugin for WordPress, Blogger...