title: Issue with length of string value

text:
# Question
Title: Issue with length of string value
I have been getting some weird issues with a sketch I put together(apart from memory low warnings, it was working fine)

When im about to post data to my php server - it seems not all the information is available

eg the output will be

```
42399.92
&HAAlt=-18.24

```

Is there a limit to string length?

Im crudely constructing a string to send to a post request

```
#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"
#include <math.h>
#include <Wire.h>
#include "MutichannelGasSensor.h"
#include "HP20x_dev.h"
#include "KalmanFilter.h"

unsigned char ret = 0;

/* Instance */
KalmanFilter t_filter;    //temperature filter
KalmanFilter p_filter;    //pressure filter
KalmanFilter a_filter;    //altitude filter

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
IPAddress server(192, 168, 0, 30); // numeric IP for Google (no DNS)
//char server[] = "www.google.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 40);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

#define Vc 4.95
//the number of R0 you detected just now
#define R0 35.54
#define DHTPIN A1     // what pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

DHT dht(DHTPIN, DHTTYPE);

int pin = 8;
unsigned long duration;
unsigned long starttime;
unsigned long sampletime_ms = 2000;//sampe 30s ;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
String data;
String data2;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  delay(150);
  HP20x.begin();
  delay(100);

  /* Determine HP20x_dev is available or not */
  ret = HP20x.isAvailable();
  if (OK_HP20X_DEV == ret)
  {
    Serial.println("HP20x_dev is available.\n");
  }
  else
  {
    Serial.println("HP20x_dev isn't available.\n");
  }

  pinMode(8, INPUT);
  starttime = millis();//get the current time;
  Serial.println("power on!");

  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

  //if (Ethernet.begin(mac) == 0) {
  //  Serial.println("Failed to configure Ethernet using DHCP");
  // try to congifure using IP address instead of DHCP:
  Ethernet.begin(mac, ip);

  // give the Ethernet shield a second to initialize:
  delay(10000);
  Serial.println("connecting...");
  //}
  // give the Ethernet shield a second to initialize:
  //delay(1000);
  //Serial.println("connecting...");

  // Serial.println(Ethernet.localIP());

  gas.begin(0x04);//the default I2C address of the slave is 0x04
  gas.powerOn();
  //Serial.print("Firmware Version = ");
  //Serial.println(gas.getVersion());
  //Serial.println("Particles\tRS\tHCHO (PPM)\tNH3 (PPM)\tCO (PPM)\tNO2 (PPM)\tC3H8 (PPM)\tC4H10 (PPM)\tCH4 (PPM)\tH2 (PPM)\tC2H5OH (PPM)");
  dht.begin();
  delay(10000);

  data = "";
  data2 = "";
}

void loop() {

  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  float HATemp2;
  float HAPres2;
  float HAAlt2;

  if (OK_HP20X_DEV == ret)
  {
    long HATemp = HP20x.ReadTemperature();
    float p = HATemp / 100.0;
    HATemp2 = t_filter.Filter(p);

    long HAPres = HP20x.ReadPressure();
    p = HAPres / 100.0;
    HAPres2 = p_filter.Filter(p);

    long HAAlt = HP20x.ReadAltitude();
    p = HAAlt / 100.0;
    HAAlt2 = a_filter.Filter(p);

  }

  duration = pulseIn(pin, LOW);
  lowpulseoccupancy = lowpulseoccupancy + duration;

  float d;

  if ((millis() - starttime) >= sampletime_ms) //if the sampel time = = 30s
  {
    ratio = lowpulseoccupancy / (sampletime_ms * 10.0); // Integer percentage 0=>100

    d = 1.1 * pow(ratio, 3) - 3.8 * pow(ratio, 2) + 520 * ratio + 0.62; // using spec sheet curve
    //Serial.print("concentration = ");

    lowpulseoccupancy = 0;
    starttime = millis();
  }
  //Serial.print("\t");
  //HCHO

  int sensorValue = analogRead(A0);
  double Rs = (1023.0 / sensorValue) - 1;

  double ppm = pow(10.0, ((log10(Rs / R0) - 0.0827) / (-0.4807)));
  //Serial.print("HCHO ppm = ");

  //MultiChannel Gas
  float NH3 = gas.measure_NH3();
  float CO = gas.measure_CO();
  float NO2 = gas.measure_NO2();
  float C3H8 = gas.measure_C3H8();
  float C4H10 = gas.measure_C4H10();
  float CH4 = gas.measure_CH4();
  float H2 = gas.measure_H2();
  float C2H5OH = gas.measure_C2H5OH();

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  data = String("dust=") + d + "&rs=" + Rs + "&hcho=" + ppm + "&nh3=" + NH3 + "&co=" + CO + "&no2=" + NO2 + "&c3h8=" + C3H8 + "&c4h10=" + C4H10 + "&ch4=" + CH4;
  data2 = String("&h2=") + H2 + "&c2h5oh=" + C2H5OH + "&temp=" + t + "&hum=" + h + "&HATemp=" + HATemp2 + "&HAPres=" + HAPres2 + "&HAAlt=" + HAAlt2;

  Serial.println(data);
  Serial.println(data2);

  if (client.connect(server, 80)) {
    Serial.println("connected");
    client.println("POST /air_add.php HTTP/1.1");
    client.println("Host:  192.168.0.30");
    client.println("User-Agent: Arduino/1.0");
    client.println("Connection: close");
    client.println("Content-Type: application/x-www-form-urlencoded;");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.println(data);
  }
  else
  {
    Serial.println("could not connect");
  }

  if (client.connected()) {
    client.stop();
  }

  Serial.println("done.");

  delay(30000);
}

```

# Answer
> 2 votes
There isn't a real limit on the length of a String that you are running into. But when you are running low on RAM (the low memory warnings you mentioned) then there is a limit on how much memory you have to put that String before you cause memory corruption and either crash the program or at best get some weird output.

The String class is particularly wasteful of memory and it has some issues when using the + operator to concatenate Strings that will leave the heap memory full of holes and reduce the amount of memory you have even further. On memory constrained systems like microcontrollers, the String class crutch is a thing to be avoided. Learn to use char arrays and real c-style strings and you'll be better off.

One place you could immediately save some memory would be by putting all the constant strings in your code into PROGMEM instead of leaving them out in RAM. So this:

```
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded;");

```

becomes

```
client.println(F("Connection: close"));
client.println(F("Content-Type: application/x-www-form-urlencoded;"));

```

# Answer
> 2 votes
There is no limit for String other than the available memory. No String can be longer that ~2KB.

In these statments

```
data = String("dust=") + d + "&rs=" + Rs + "&hcho=" + ppm + "&nh3=" + NH3 + "&co=" + CO + "&no2=" + NO2 + "&c3h8=" + C3H8 + "&c4h10=" + C4H10 + "&ch4=" + CH4;
data2 = String("&h2=") + H2 + "&c2h5oh=" + C2H5OH + "&temp=" + t + "&hum=" + h + "&HATemp=" + HATemp2 + "&HAPres=" + HAPres2 + "&HAAlt=" + HAAlt2;

```

you are creating and deleting a lot of temporary Strings. How much memory will you need to hold all those temp Strings togheter? You don't know. Hard to say, because it's implementation dependent.

In Arduino you have to learn to use `char` arrays (and pointers) to works with strings. With a low level C method you know exactly how much space you will need. If you don't have enough memory, you still can reorganize your sketch to work around limitations.

Arduino is a low level world.

---
Tags: string, memory
---


# Question
Title: Connection of serial pin on ATMEGA328P to connector B type(female) D+ and D-
Now I am using ATMEGA328P and want to communicate with computer in serial using B connector female (also want to provide power from computer to ATMEGA328P using B connector).

So here is my question. How can I connect D+ and D- to ATMEGA328P's pin No. 2 (PD0/RX) and pin No. 3 (PD1/TX)? In other words which pin should I connect to D+ and D- ? Also is it possible to connect them directly?

Thank you in advance.

# Answer
The Arduino Uno (and similar boards) have a chip on them that does serial communication via a dedicated USB chip (the ATmega16U2 chip). You can't just connect pins 2 and 3 to D+ and D- and turn that into USB.

You can conceivably do it using V-USB which does USB in software in conjunction with a few extra components.

You can also buy external serial to USB adapters which do the conversion externally. If you Google `serial to usb adapter` you will find quite a few for around $3.
> 2 votes

---
Tags: serial, atmega328
---


# Question
Title: ATtiny85 MIDI IN using SoftwareSerial - Is it possible?
I'm trying to read MIDI notes using an ATtiny85 via SoftwareSerial; so far, I've got no success at all.

* The ATtiny is using the internal clock, 8Mhz, 5V.
* The MIDI keyboard sends clean note on/off messages (tested with a MIDI monitor).
* I've tried several test programs, particularly Nick Gammon's, as described here.
* I've followed Nick's schematics to build the connector, and I can confirm his program works with an Arduino, but not with the ATtiny.
* I calibrated the Tiny with OSCCAL - still no luck.
* As far as I can tell, `midiSerial.available()` is always 0, in every program I've tried.
* SoftwareSerial works fine for the purpose of MIDI out.

That's as far as I've got. I'd appreciate any pointers, especially if anyone else could try Nick's code and see if it works.

**EDIT**

There was an issue with my local version of SoftwareSerial. I had commented out code so that I could use pin change interrupts in my MIDI out program, but those interrupts are essential for MIDI input. Once I uncommented the code, all the programs I tested worked, so it is indeed possible to receive MIDI using an ATtiny.

That said, I've tried four keyboards as inputs and only one has worked so far, a Korg 01/W proX. The same happens when I load the MIDI code directly in the Arduino, so based on the answers below I will try a different optocoupler, and post my results here for the sake of improving on the answer.

Here's the schematics I was talking about:

# Answer
I have done tests with a normal Arduino with software serial and missed quite a bit of messages, so I would advice to use hardware serial's instead.

For MIDI Out probably it works, but for MIDI In I have bad experiences. Especially when it comes to many messages, like aftertouch/pitch bend etc.

Also, I tried some optocouplers which didn't work well (like 6N137). I now use H11L1 optocouplers (they are white compared to most black ICs) and work perfectly.

You already checked the clean note on/note off ... are you sure it doesn't send active messages?

Below the schematic as I use it.. It does not matter if an STM32 or Arduino is used. Actually I haven't checked if H11L1 are 3.3V or 5V but they seem to work on both.

simulate this circuit – Schematic created using CircuitLab
> 1 votes

---
Tags: midi, attiny85
---


# Question
Title: How to filter data of ultrasonic sensor using arduino
I am working on a project that uses ultrasonic HC-SR04 sensor to measure distance. I've connected the sensor to the Arduino Uno board, I am also using Arduino IDE to program the Arduino. The problem is that I am not getting stable readings, there is the presence of some noise in the readings. How should I filter out this noise.

Circuit connections are as follows:

```
Ultrasonic sensor | Arduino
VCC                 5V
GND                 GND
Trigger             12 (digital)
Echo                11 (digital)

```

I am using new ping to measure distance.

# Answer
> 1 votes
Having noise in the sensor data is common and normal, especially on low end sensor.

As you can imagine there is a "data science" to clean up the values, mostly based on statistic.

One of the easiest trick is to remove all values that have a difference \> Xt from previous value; as you can imagine the tolerance Xt is calculated by a factor X, let's say a bit more the maximum speed you think the system will operate in time multiplies by the time since the last valid measure.

For example you expect a max distance chance of 10m in one second, so we use as tolerance 15m/s. If we don't read valid value, after 6 seconds our tolerance is 15m/s * 6s = 90m

Of course after much time maybe you just want to accept the first reading that seems valid, so you analyze 10, 100, 1000 value and see if they seems correct.

Then for a quick and dirty smoothing and cleaning if the remaining data, you can run a average on the value, there are different forms, and more value you use more stable are your output, but also less fast to follow changes.

A kallmann filter is the most common but also complex way to clean the data, but I guess in most hobbyist application is just overkill.

# Answer
> 0 votes
First off, Start a time counter. You can get the current time instant by using:

```
unsigned long StartTime = millis();

```

After that, keep counting time by using the following block:

```
 unsigned long elapsedTime = currentTime - StartTime;

```

With this you can know how much time (in ms) has passed since StartTime. In this time loop, keep reading data from the Ultrasonic sensor. As the sensor may need some time to get each reading, take a conservative delay (e.g. 100 ms). You can again increment the timer by using a goto statement. Note that you must also maintain a counter for later. In this time loop, keep adding the readings from the sensor to itself. Finally when the stipulated time is over, you can take the average by dividing the final sum of the sensor values with the counter maintained earlier.

---
Tags: arduino-uno
---


title: Hall effect sensor

text:
# Question
Title: Hall effect sensor
I've taken velocity readings of a wheel using a hall effect sensor. Is it possible to take the **acceleration** readings corresponding to those velocities as well? Do I just have to divide the velocity by the time taken?

# Answer
Yes, you can divide speed by time and get acceleration, obviously that will give you the mean acceleration between speed reading.
> 0 votes

---
Tags: arduino-uno, sensors
---


# Question
Title: My Arduino isn't showing up on my computer. What should I do?
You have your spangly new Arduino board, you've installed the software, and you plug it in. Nothing happens. Diagnosing problems may be a little tricky for the beginner, so what hoops should you go through to try and work out where the problem is and why you can't get your board working?

---

Note: this is an FAQ. Do not use it to ask about your specific problem. Comments will be deleted. Either ask a new question detailing all the steps you have taken, or, better, discuss your problem in chat.

# Answer
# Linux

Getting Arduino and Arduino-like boards working properly under Linux can be a troublesome task if you are not familiar with how Linux works.

So I am going to introduce you to some of the basic tools you will need to work out why your board isn’t working as you’d like it to work.

The majority of problems a new user is faced with boil down to one simple thing: permissions. A normal user doesn’t, by default, have permission to talk to much in the way of hardware. That include serial ports and USB serial emulation ports (FTDI, etc). Fortunately granting permission to your user to access the serial ports is a simple matter, and one that you should always do by default to make life much simpler for you. This is done through a Linux permission facility called groups. A group is a name given to a collection of users, files, devices etc., that go together. Permissions can be granted on files to the group that it belongs to, and users that are in that group can access those files. On Linux most devices are just files.

The serial ports are all in the default group dialout, but your normal user isn’t. So you need to add your user to that group. Only root has the permission to do that, so use sudo to execute the commands as root. If you don’t have access to root through sudo or some other means then you will have to seek the assistance of an administrator who does have permission.

First add your user (you’re called fred, right?) to the dialout group:

```
$ sudo usermod -a -G dialout fred

```

The “-a” is of vital importance. That says “Add the user to these groups”. Without it you have “Set the user’s groups to be this list”. If you omit the “-a” by accident you will remove your user from any other groups before setting them to be in the dialout group. That means you will never be able to run sudo again. So be sure not to miss that one out, eh?

Now that has only changed the configuration files (/etc/groups in this case) and the change hasn’t actually had any effect yet. The groups are only set when you log in to the computer, so you will either need to log out and back in again or (a more definite solution, especially if you have auto-login turned on) reboot the computer.

Now you should be in the dialout group:

(before)

```
$ id
uid=1000(fred) gid=1000(fred) groups=1000(fred),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare)

```

(after)

```
$ id
uid=1000(fred) gid=1000(fred) groups=1000(fred),4(adm),20(dialout),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare)

```

Now try it, see if that has got you working.

Next step, if that doesn’t fix your problems, is to see if the computer knows about the Arduino. First look to see if there is a device file for the Arduino and if it has the right permissions:

```
$ ls -l /dev/ttyACM* /dev/ttyUSB*
ls: cannot access '/dev/ttyUSB*': No such file or directory
crw-rw---- 1 root dialout 166, 0 Aug 24 11:05 /dev/ttyACM0

```

Don’t worry about the “cannot access…” message unless you get two of them. There are two sets of device names that the Arduino could be under depending on which type it is. Genuine UNO and similar boards (the ones with the ATMega16U2 chip as the USB interface) are /dev/ttyACM\*. Clones using the CH340G, and FT232 based boards appear as /dev/ttyUSB\*. We just asked for both there, so it complained when it couldn’t find any entries for one of the types. Which is fair enough. If you get:

```
$ ls -l /dev/ttyACM* /dev/ttyUSB*
ls: cannot access '/dev/ttyACM*': No such file or directory
ls: cannot access '/dev/ttyUSB*': No such file or directory

```

however, then worry. That can’t find anything at all. The computer doesn’t know the Arduino exists.

One thing to check is the permissions on the files that have been listed above. You see the word “dialout” in the list? That’s the group that the device belongs to, and it should match the group you added yourself to earlier. There are times when it may say “tty” instead. This is because some other program has grabbed the device and is using it to allow remote access to the computer through that serial port. For that you need to make sure that there are no agetty processes using that port. How you do that is very much down to your version of Linux. Modern Debian based systems (such as Debian Jessie and Ubuntu) use systemd to manage it. You can examine what is running on your system using

```
$ systemctl status

```

That returns a tree of all the services that your system is running. Look through that list to see if there are any “serial-getty” services running. If there are you should remove them:

```
$ sudo systemctl stop serial-getty@ttyACM0
$ sudo systemctl disable serial-getty@ttyACM0

```

While you’re there look through the list for another troublesome member: ModemManager. This is a program used to dial out to the internet through old-fashioned modems (you know, the kind that go beep beep beep squark squeek squark ) or USB DSL modems (that no one uses any more, we’re all wireless now. Remember the old green “frog” modem from way back when…?). That has a habit of grabbing a serial port as soon as it appears and trying to configure a modem attached to it. While that’s happening nothing else can access the serial port. It’s especially a problem with boards like the Arduino Leonardno or chipKIT Lenny which create a new serial port when they enter programming mode. So unless you really need the functionality that ModemManager provides you should just remove it:

```
$ sudo apt-get remove modemmanager
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED
modemmanager
0 to upgrade, 0 to newly install, 1 to remove and 138 not to upgrade.
After this operation, 2,826 kB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 180827 files and directories currently installed.)
Removing modemmanager (1.4.12-1ubuntu1) ...
Created symlink from /run/systemd/system/ModemManager.service to /dev/null.
Removed symlink /run/systemd/system/ModemManager.service.
Processing triggers for man-db (2.7.5-1) ...
Processing triggers for hicolor-icon-theme (0.15-0ubuntu1) ...
Processing triggers for dbus (1.10.6-1ubuntu3) ...

```

Another useful tool is `lsusb`. This lists the USB devices that are connected to your computer. You should find your Arduino listed there. This is especially useful for boards that don’t present a serial port for programming, but we’ll cover those in more depth a little later on.

```
$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 002: ID 80ee:0021 VirtualBox USB Tablet
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

```

Nothing much on this computer. Well, actually I am using a virtual machine for this, as you can see by the VirtualBox USB Tablet entry. But let’s connect my Arduino UNO:

```
$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 004: ID 2341:0001 Arduino SA Uno (CDC ACM)
Bus 002 Device 002: ID 80ee:0021 VirtualBox USB Tablet
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub

```

So we know that the operating system has identified it. You can find out lots more information about it if you so desire, with:

```
$ lsusb -v -d 2341:0001

```

That gives you all the device descriptors for the device. Tells you all you could ever want to know about the information the board sends to the computer when it is connected.

Another useful tool is dmesg. This is the system message log viewer and can be used to find out exactly what is going on with your computer. It has a useful “keep running” mode specified by the flag `-w`. I like to run `dmesg` and then press `return` a few times to create a breakpoint in the listing, then plug in the board I am diagnosing to see just what the messages are that pertain to that board:

```
$ dmesg -w
[ 0.000000] Initializing cgroup subsys cpuset
[ 0.000000] Initializing cgroup subsys cpu
[ 0.000000] Initializing cgroup subsys cpuacct
[ 0.000000] Linux version 4.4.0-24-generic (buildd@lgw01-12) (gcc version 5.3
... lots of stuff spools past ...
[ 19.425870] NFS: Registering the id_resolver key type
[ 19.425883] Key type id_resolver registered
[ 19.425885] Key type id_legacy registered
<press return a few times>

<connect Arduino>
[ 131.232511] usb 2-2: new full-speed USB device number 3 using ohci-pci
[ 131.519307] usb 2-2: New USB device found, idVendor=2341, idProduct=0001
[ 131.519312] usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=220
[ 131.519315] usb 2-2: Product: Arduino Uno
[ 131.519317] usb 2-2: Manufacturer: Arduino (www.arduino.cc)
[ 131.519319] usb 2-2: SerialNumber: 64934333235351B002E0
[ 131.605485] cdc_acm 2-2:1.0: ttyACM0: USB ACM device
[ 131.613792] usbcore: registered new interface driver cdc_acm
[ 131.613796] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters

```

Press `CTRL`+`C` to terminate the listing.

If nothing appears when you connect the Arduino then there is something fatally wrong. My money is either on a faulty cable (some cheap cables on eBay only have power wires and no data wires) or the Arduino itself is completely dead.

More likely though you get error messages. I can’t replicate them right now (I don’t have a faulty Arduino) but they usually pertain to things like “device not accepting address” and “Error -71”. This is usually caused by bad USB communication. It may, again, be a faulty cable, or it may be that the USB interface chip is not working right. Maybe the crystal has drifted and it’s not running at precisely the right frequency. Maybe it’s a cheap CH340 chip that has failed. Maybe the firmware on the ATMega16U2 has become corrupted.

If all that checks out fine so far and you still can’t upload a sketch there may be something even more fundamentally wrong with your Ardunio board. Maybe it’s time to look to see if there is actually any communication happening. I find the program minicom invaluable, although you could use the ghastly Serial Monitor in the Arduino IDE.

First you need to disable the main chip on your Arduino. You can do this simply by connecting the RESET pin to GND with a piece of wire. Or you can hold the RESET button down, but that means you’re then typing one-handed. That stops the main chip from interfering in any way with the serial communications while you’re testing it. It can also prevent any damage to the chip while testing.

The second step is to connect the TX and RX pins together (pins 0 and 1 usually) with another piece of wire. You are now in what is known as a loopback mode. Any serial data you send to the board should be sent straight back to where it came from. So now fire up `minicom` and try it out (you will need to install it first of course with `sudo apt-get install minicom`).

```
$ minicom -D /dev/ttyACM0 -b 115200

```

Of course replace the `/dev/ttyACM0` with the real device name for your board. The `-b 115200` is the baud rate, though that is pretty much irrelevant since we’re not actively communicating with an external device.

Now you should be able to type some gibberish and get some gibberish back. If you do then communication is working fine, and the problem most likely lies within the target MCU – maybe the bootloader has become corrupted and needs re-flashing, or the chip has died.

`CTRL`+`A`, `X` gets you back out of `minicom` again.

If you didn’t get your gibberish back, or you got back different gibberish than you sent, then there is definitely something up with the serial communications. The main culprit is, again, the USB interface chip.

All this, of course, has been focusing on the traditional Arduino style interface with a USB serial port of some form. Not all boards give you that though. Some, such as the Launchpad boards, use a hardware programmer on the board, and that doesn’t always give a serial port – certainly the programming isn’t done through the serial port. To gain access to those (again by default you don’t have permission to access the low-level devices) you have to get deeper into the operating system.

For these you need to make modifications to the configuration of a system called `udev`. This is a management system for the devices on your computer and is used to control what happens to them when they are plugged in. One of the things you can do is set the permissions on that device so people can actually access it.

The system is configured by a set of rules in “rules files”. These are stored in `/etc/udev/rules.d` and there should already be a few in there for other purposes. You will need to, as root, create a new file to put your rules in.

```
$ sudo nano /etc/udev/rules.d/50-boards.rules

```

The most important thing to know is the VID and PID (Vendor ID and Product ID) of your board. You can get that from `lsusb` as above (it’s the vvvv:pppp bit – v = VID, p = PID). You can then feed that into a simple rule, such as:

```
ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="0033", MODE="0660", GROUP="plugdev"

```

There we are matching a vendor (`04d8` = Microchip) and product (`0033` = PICkit2) and assigning it to the group `plugdev` (make sure you are in that group – default users should be already) and allowing the group members read and write access to it (`MODE="0660"`). Save that file and restart `udev` for the changes to take effect (or reboot your computer if you like):

```
$ sudo /etc/init.d/udev restart

```

Now plug the device in and you should have permission to access it.
> 4 votes

# Answer
# Windows

Ok, the first thing you need to do is identify the kind of board you have. I don't mean is it an Uno, a Mega, or one of Intel's brief forays into the Arduino world - I mean what kind of USB interface the board has.

Boards can be loosely grouped into two categories - CDC/ACM and Custom USB Profile.

Current versions of the official Arduino Uno and Mega are both CDC/ACM devices (CDC/ACM, by the way, stands for Communication Device Class / Abstract Control Model). This means that between the main microcontroller (ATMega328P, ATMega2560, etc) there is another microcontroller - usually an ATMega16U2. This takes the serial data from the main microcontroller and sends it to the PC over USB using the USB CDC/ACM protocol.

You can identify these boards by the chip that is nearest the USB connector.

That small black chip with the Atmel logo on it is the USB interface chip.

Boards of this style have the USB drivers included in the Arduino IDE. If you have installed the IDE properly the drivers should have been installed for you automatically. Also Windows 10 (finally) contains "class compliant" drivers for CDC/ACM, so no drivers actually need to be installed for these boards anyway (installing the Arduino ones doesn't hurt, though, and helps it to show the right board name in the device manager).

The other category of board, "Custom USB Profile", use different chips. And there's lots of different chips it can be using.

Early Arduinos and some other makes of board (such as chipKIT boards from Digilent) use the Future Technologies RT232R chip. This sometimes looks at first glance like the Atmel chip above, but is very very different. You have to check the number on it to be sure. Other times a different package is used and it's obvious it's different.

Then there are the cheap clone boards you get on eBay and Amazon. These, more often than not, use yet another chip - the CH340G. This is a fraction of the cost than the FT232R or ATMega16U2 chips, which is why cheap boards tend to use them. They're also less reliable and harder to get working in some operating systems (most notably OS X).

For these kind of chips you need to install the right driver for the specific chip you have. For the FT232R chips that's not too hard - Future Tech have the drivers on their website.

The CH340G driver is harder to find from a reliable source.<sup>(someone with more knowledge please fill this in)</sup>

Ok, so your board is a CDC/ACM based one, or you are sure you have installed the right drivers. The next thing to check is if the board is even being seen by the operating system.

When you plug it in for the first time it should pop up a bubble saying it found new hardware:

Or something similar to that. It should also make a noise.

If neither of those happen then it's likely the computer isn't seeing the device at all. This sounds more like a hardware problem than a Windows problem.

The first thing to try is to plug the Arduino into a different port. Even better is, if you have access to one, plug the Arduino into an entirely different computer. What you are trying to diagnose here is if the problem is related to the Arduino/USB cable or to the computer/USB port.

If it works in a different computer but not your computer then there is something wrong with your USB ports. It may be that you are using a computer that is locked down (such as in a school or an office) where the "group policy" has been configured to not allow any USB devices to be used (chiefly to prevent people bringing in memory sticks with viruses on them). If this is the case you will have to consult with the powers that be in your organisation to get your device authorised. If not, then you may have more luck plugging your Arduino into a *powered* USB hub instead of directly into your computer (it may be that the computer can't provide enough current for the Arduino to run properly).

If it's failing equally as badly on another computer / port then you should start looking at the USB cable and the Arduino.

It is quite common for small battery powered devices that are charged through USB to be shipped with cheap cables. These are cables that only have the +5V and GND wires in them - no D+ and D- wires. For obvious reasons these can't be used with an Arduino: with no data wires how can the computer see it? However it's generally impossible to tell these cables apart from a full USB cable.

If you happen to be using one of these cables without knowing it you'll get a power light on the Arduino and not much else. So you need to swap the USB cable for another one - preferably one you know works properly. If it suddenly springs into life then you know you have a bad cable. Throw the old one away - it's almost useless - or stick a label on it saying "power only" so you know what it is.

Another thing to mention in passing: I have known some of the early Arduino boards to have lower quality USB connectors on them (Uno R2). These sometimes have trouble connecting properly with some cables (not all), so maybe a better quality cable may fix the problem, or tweaking the contacts within the USB connector with a needle may help.

Right - the other direction to go in is if it does identify that you plugged a board in, but can't install the drivers. Maybe it's saying the device failed, or it can't find the drivers.

Step one is to go into the device manager and look for an "Unknown Device":

That is most likely the Arduino. You should be able to inspect all the data for it to find the USB Vendor ID (VID) and Product ID (PID). If those are showing up as corrupted, not available, or `FFFF`, then your Arduino has a serious problem and Windows cannot work out what it is. In this case it may be that the USB interface chip has failed (there have been unsubstantiated rumours that shorting 5V to GND on an Uno R3 can kill the ATMega16U2 chip). There's not much you can do when that has happened.

If you can find reasonable data in the device manager, as in the image below, then it's going to be a case of trying to fix (or find the *correct*) driver, since you may not have the right one installed. Also note that some other board manufacturers use the same VID and PID as the Arduino and having their drivers installed could cause the Arduino to not function properly - you should uninstall their drivers first, or manually set the Arduino to use the proper Arduino driver.
> 2 votes

# Answer
**MAC OS X**

Getting an Arduino to work with OS X can be as simple as "plug and play" to almost impossible. It depends on which version of OS X you are using, and more importantly, what "kind" of Arduino you have. "Kind" means, is your Arduino a genuine Arduino, a "counterfeit" claiming to be the real thing or a cheap "knock off" using the CH340 USB interface.

Your first step is to identify exactly what you've got. If you have a CH340 chip on your Arduino like the one on this cheap "knock off" of an Arduino UNO, then you'll need to install a driver so that OS X can communicate with it.

Download a CH340 driver for the exact version number of OS X you are using and install it as per the manufacturer's instructions. If you download a driver for the latest version of OS X and install it on an older version of OS X, it may install just fine. When you plug in your Arduino device, OS X reads the .kext file and sees it's for a newer version of the OS so it doesn't load it, and never will (unless you upgrade to that exact version someday).

Here is a picture of a "counterfeit" Arduino UNO. It uses the ATMEL MEGA 16U2 chip as a USB interface which is "suppose to work" with OS X's existing drivers. If you have one of these and it's "not working", don't bother installing the FTDI drivers because that won't solve your problem.

Unfortunately, what works on one version of OS X doesn't always work on another version. Solutions are usually specific to OS X versions, but there are some common troubleshooting steps to follow once you know what Arduino USB Interface you have and you've installed the correct driver (if required).

Verify that OS X is recognizing your device.

1. Click on the Apple icon in the top left corner of the menu bar.
2. Click on "About This Mac".
3. Click on the "More Info…" button (OS X 10.6) or "System Report…" button under the "Overview" section (OS X 10.7 and higher).
4. Expand the "Hardware" menu item and click on USB.

You should see something like this:

If you plugged in your Arduino and is not listed somewhere on the USB Device Tree, I can guarantee it will not show up in the Arduino IDE Serial Port Menu. It won't show up in the /dev list using terminal either. Please keep in mind that System Profiler does not refresh automatically. If you un-plug your Arduino and plug it back in again, you have to MANUALLY re-fresh System Profiler to see the current data

If you still can't get your Arduino working, try swapping the USB cable or using a different computer / OS to see if it works.
> 0 votes

---
Tags: hardware, linux, osx, windows, faq
---


# Question
Title: Store 3-dimensional char array in program memory with PROGMEM
I'm running a sketch whith a quite big, constant three dimensional char array (or simply, a table of strings). As this takes lots of RAM I'd like to store it in programm memory in order to keep the RAM free. This is the Sketch I'd like to run (my first idea):

```
const char *rules [16][5] PROGMEM ={
 {"1Lorem ipsum dolor sit amet, consetetur sadipscin0g elitr", "sed di7am nonumy 8eirmod tempor invidunt"},
 {"2Lorem ipsum dolor sit amet, consetetur sadipscing2 elitr", "sed diam nonumy 9eirmod tempo5r invidunt"},
 {"3Lorem ipsum dolor sit amet, consetetur sadipscing 4elitr", "sed0 diam nonumy 1eirmod tempor invidunt"},
 {"4Lorem ipsum dolor sit amet, consetetur sadips3cing elitr", "sed diam nonumy 2eirmod tempor in5vidunt"},
 {"5Lorem ipsum dolor sit amet, consetetur 3sadipscing elitr", "sed 7diam nonumy 3eirmod tempor invidunt"},
 {"6Lorem ipsum dolor sit amet, conset9etur sadipscing elitr", "sed diam n8onumy 4eirmod tempor invidunt"},
 {"7Lorem ipsum dolor sit amet, consetetur sadipsc3ing elitr", "sed diam nonumy 5eirmod tempor inv4idunt"},
};

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(115200);
  for(int i = 0; i < 16; i++){
    for(int j = 0; j < 5; j++){
     Serial.println(rules[i][j]);
    }
  }
}

void loop() {
}

```

But this fails with the following error:

```
variable 'rules' must be const in order to be put into read-only section by means of '__attribute__((progmem))'

```

According to Arduino's FAQ this can be solved by changing the array's declaration to

```
const char * const rules [16][5] PROGMEM ={

```

The sketch compiles now without errors, but the global variables (here only the array) still need lots of RAM:

```
Sketch uses 2484 bytes (7%) of program storage space. Maximum is 32256 bytes.
Global variables use 880 bytes (42%) of dynamic memory, leaving 1168 bytes for local variables. Maximum is 2048 bytes.

```

I'm not sure what causes this, but is it possible that only the pointers to the strings are stored in program memory, while the strings theirselves are still copied to RAM?

How do I put the whole arary in programm memory?

There has already been a question like this on StackOverflow, but the answer recommends using prog\_chars, which are according to the AVR documentation deprecated.

# Answer
Here you have:

```
const char * const rules [16][5] PROGMEM ={

```

One `const` refers to the `char*` part. It means *the pointer value doesn't change*.

The other `const` refers to what it is pointed by. It means *the thing this pointer points doesn't change* (but the pointer itself can change).

In your sketch the thing that get put into PROGMEM is the **pointer array**; the texts themselves are still in RAM. Have a look at PROGMEM page and study this example:

```
const char string_0[] PROGMEM = "String 0";   // "String 0" etc are strings to store - change to suit.
const char string_1[] PROGMEM = "String 1";
const char string_2[] PROGMEM = "String 2";
const char string_3[] PROGMEM = "String 3";
const char string_4[] PROGMEM = "String 4";
const char string_5[] PROGMEM = "String 5";

// Then set up a table to refer to your strings.

const char* const string_table[] PROGMEM = {string_0, string_1, string_2, string_3, string_4, string_5};

```

You need to declare each string by itself, and later put them in an array.

As you can see, pointer are inescapable.

**Edit: The F() Macro**

Here is a proof of concept of how to manage an array of string (lowercase "s") and strings constants in PROGMEM:

```
const char uno[] PROGMEM = "1Lorem ipsum dolor sit amet, consetetur sadipscin0g elitr";
const char dos[] PROGMEM = "2Lorem ipsum dolor sit amet, consetetur sadipscin0g elitr";
const char tre[] PROGMEM = "3Lorem ipsum dolor sit amet, consetetur sadipscin0g elitr";
const char cua[] PROGMEM = "4Lorem ipsum dolor sit amet, consetetur sadipscin0g elitr";
const char cin[] PROGMEM = "5Lorem ipsum dolor sit amet, consetetur sadipscin0g elitr";

const char * const rules [3][2] = {
 {uno, dos},
 {tre, cua},
 {cua, cin}
};

void setup() {
  Serial.begin(115200);
  Serial.println(F("PROGMEM string test"));
  Serial.println(F("-------------------"));
  for(int i = 0; i < 3; i++){
    for(int j = 0; j < 2; j++){
      Serial.print(i); Serial.print(" "); Serial.print(j); Serial.print(" ");
     Serial.println((const __FlashStringHelper *) rules[i][j]);
    }
  }
}

void loop() {
}

```

Use the **F()** macro to put **constant** strings in PROGMEM. Otherwise, the strings will live in RAM. Are running short of RAM? Put the F() macro around those pesky strings and send them out of your RAM.
> 1 votes

---
Tags: arduino-uno, array, sram, progmem, c-string
---


title: Cannot find crtatmega328p

text:
# Question
Title: Cannot find crtatmega328p
I have just installed the arduino IDE on my gentoo laptop. I then installed the toolchain with the following command:

```
crossdev -s4 avr

```

This does a stage 4 install of the toolchain, with all its dependecies etc.

I am following a tutorial, with my arduino uno, which I am assuming is using the Amtel ATmega328 chipset. This tutorial is simply going to turn an led on and off.

I click the verify button and I get the following errors:

```
/usr/libexec/gcc/avr/ld: cannot find crtatmega328p.o: No such file or directory

/usr/libexec/gcc/avr/ld: cannot find -latmega328p

collect2: error: ld returned 1 exit status

```

I downloaded the source files "avr-libc" compiled them and noticed inside the directory

```
/avr-libc-1.8.1/avr/lib/avr5/atmega328p

```

That the compiled module is named crtm328p.o which is available on my pc under the directory:

```
/usr/avr/lib/

```

I dont understand what is wrong and why this is not working. The only posts relating to missing files are over 5 years old and those solutions are not working.

# Answer
I do not use Gentoo, but I can tell you how things work on Ubuntu 14.04, which also installs Arduino 1.0.5. I assume Gentoo should not be that different... The dependency tree is like this:

* The package named "arduino", version 1.0.5, contains the IDE. It depends on Java and on arduino-core.
* arduino-core, version 1.0.5 also, has the Arduino core libraries. It depends on gcc-avr, avrdude and avr-libc.
* gcc-avr comes as version 4.8, avr-libc is version 1.8.0.

When you compile a program for an Uno, on the link stage avr-g++ gets called, and it is asked to link your object files with libcore.a (the Arduino core) and -lm (the math library). avr-g++ in turn calls the linker (collect2) and asks it to link together /usr/lib/avr/lib/avr5/crtm328p.o (the C runtime), your object files, libcore.a, -lgcc (the GCC support libraries), -lm and -lc (avr-libc). Notice that there is no crtatmega328p.o, it's called crtm328p.o instead. And there is no -latmega328p.

For diagnosing your problem, you can set an IDE preference so that it shows you the exact command lines it runs for compiling your program. You can then copy and paste the last command (link stage) on a terminal. If you add the `-v` option (meaning “verbose”), you will see what gcc is asking the linker to put together.

You may be able to fix your problem with a simple symlink from crtm328p.o to crtm328p.o. If you are using vanilla Gentoo packages, you should then file a bug to the packager. On Ubuntu, you type `sudo apt-get install arduino` and it just works.
> 2 votes

# Answer
I had the same error, but I did everything as says in arduino wiki: https://wiki.gentoo.org/wiki/Arduino#Install\_the\_toolchain\_using\_crossdev

and solved it, after read this discussion: https://wiki.gentoo.org/wiki/Talk:Arduino

**helped**: build the avr toolchain without '--stable' part of crossdev command line :

USE="-openmp -hardened -sanitize -vtv" crossdev -s4 --portage --verbose --target avr
> 1 votes

---
Tags: arduino-uno, arduino-ide, compile
---


# Question
Title: Which of the clock sources are actually internal to the ATmega328P?
I am trying to determine exactly what all of the fuse settings correspond to on the ATmega328P. The options for the `CKSEL` bits are:

* 1111-1000: Low Power Crystal Oscillator
* 0111-0110: Full Swing Crystal Oscillator
* 0101-0100: Low Frequency Crystal Oscillator
* 0011: Internal 128kHz RC Oscillator
* 0010: Calibrated Internal RC Oscillator
* 0000: External Clock

Clearly, the clocks labeled "internal" are part of the "guts" of the microcontroller. No external components are required to use those clock sources. The external clock is clearly an external source.

My question is this: are the low power, full swing, and low frequency sources built in to the microcontroller itself, or do they require an external crystal oscillator to be purchased and connected to the clock pins? This is not 100% clear from reading the data sheet. (Although I am under the impression that they are not internal to the device.)

# Answer
If it requires a crystal, as the ones in question clearly state, then obviously you need to connect a crystal (and load capacitors). Or use a ceramic resonator.

Only those labelled as internal are internal. *Everything* else is external.
> 4 votes

---
Tags: atmega328, oscillator-clock, fuses
---


title: PROGMEM on Arduino Due (ARM Cortex M3)

text:
# Question
Title: PROGMEM on Arduino Due (ARM Cortex M3)
I've been working with the `PROGMEM` function on my Arduino Uno and I'm thinking about changing to the bigger Arduino Due which is based on a ARM Cortex M3 processor instead of the AVR processor on the Uno.

The Arduino Reference now says:

> PROGMEM is part of the pgmspace.h library that is available in the AVR architecture only.

My question now is: Is it possible to store constants in program memory instead of flash on the Due?

If found this post on the Arduino forums, suggesting that this is done automatically. Does anyone know something about that?

EDIT: Arduino IDE doesn't give stats about RAM usage after compiling, so I can't just try.

# Answer
> 2 votes
There is no PROGMEM required on ARM. There is no memory bus separation between Flash and RAM, so it doesn't copy constants from flash to RAM which PROGMEM is used to prevent.

---
Tags: arduino-due, flash, progmem, sam3x
---


# Question
Title: Cannot get any readings from DHT22 Sensor connected to Arduino M0 Pro
I have directly connected a DHT22 to my Arduino M0 Pro. I have put a 10k Resistor across the Data & V+ of the sensor pinout. The data is connected to pin 5 and I have it connected to 3.3V output of Arduino.

I am just trying to see some readings from the sensor via the SerialUSB console. I have downloaded and unpacked the DHT libraries that I got on the Adafruit website/GitHub. As far as I know I have these libraries in the right directory - C:\Program Files (x86)\Arduino\libraries\DHT... I also have installed the Adafruit Unified Sensor library on the IDE.

I am trying to use the Tester program that is provided, this compiles ok but the SerialUSB printout prints:

```
Failed to read from DHT sensor!
Failed to read from DHT sensor!
Failed to read from DHT sensor!

```

And so on...

I have tried another DHT22 sensor to no avail.

The only part of the code I have changed is the pin number and changed from Serial to SerialUSB.

Not sure if I have to change anything else? Or am I doing something really silly!

---

Here is the code:

```
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain

#include "DHT.h"

#define DHTPIN 5     // what digital pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// Connect pin 1 (on the left) of the sensor to +5V
// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1
// to 3.3V instead of 5V!
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor

// Initialize DHT sensor.
// Note that older versions of this library took an optional third parameter to
// tweak the timings for faster processors.  This parameter is no longer needed
// as the current DHT reading algorithm adjusts itself to work on faster procs.
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  SerialUSB.begin(9600);
  SerialUSB.println("DHTxx test!");

  dht.begin();
}

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    SerialUSB.println("Failed to read from DHT sensor!");
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  SerialUSB.print("Humidity: ");
  SerialUSB.print(h);
  SerialUSB.print(" %\t");
  SerialUSB.print("Temperature: ");
  SerialUSB.print(t);
  SerialUSB.print(" *C ");
  SerialUSB.print(f);
  SerialUSB.print(" *F\t");
  SerialUSB.print("Heat index: ");
  SerialUSB.print(hic);
  SerialUSB.print(" *C ");
  SerialUSB.print(hif);
  SerialUSB.println(" *F");
}

```

# Answer
> 1 votes
Found what the fault was, it was a solder short between pins on a custom PCB that was sitting on top of the Arduino board. My bad! At least I have it working now!

---
Tags: arduino-ide
---


title: Arduino Mega 2560 + BL-TB6560+ Stepper

text:
# Question
Title: Arduino Mega 2560 + BL-TB6560+ Stepper
I am working on an Arduino Mega 2560 and BL-TB6560-V2.0 driver for running a stepper motor. Initially I was able to run the stepper in both direction with code given and a schematic as per this image.

.

```
int dirpin = 7;
int dirH = 12;
int steppin = 9;

void setup() {
  pinMode(dirpin, OUTPUT);
  pinMode(steppin, OUTPUT);
  pinMode(dirH, OUTPUT);
}

void loop() {
  digitalWrite(dirpin, LOW); // Set the direction.
  digitalWrite(dirH, LOW);
  delay(1000);
  for (int i = 0; i<32767; i++) {
    // Iterate for 4000 microsteps.(32767)
    digitalWrite(steppin, LOW); // This LOW to HIGH change is what creates the
    digitalWrite(steppin, HIGH); // "Rising Edge" so the easydriver knows to when to step.
    delayMicroseconds(50);
  }
  delay(1000);
  digitalWrite(dirpin, LOW); // Set the direction.
  digitalWrite(dirH, HIGH);
  for (int j = 0; j<32767; j++) {
    // Iterate for 4000 microsteps.
    digitalWrite(steppin, LOW); // This LOW to HIGH change is what creates the
    digitalWrite(steppin, HIGH); // "Rising Edge" so the easydriver knows to when to step.
    delayMicroseconds(50);
  }
}

```

But now when I am trying it, the same stepper is not rotating. What could be the reason? I have checked the driver voltage, it's ok, and the code and schematic is the same.

# Answer
I just removed the ENE(+) pin of the driver, which was connected to the +5V. Now it's working and the stepper is rotating.

I just got a hint from this video.
> 1 votes

---
Tags: stepper
---


# Question
Title: The most efficient way to test whether a byte is "empty" (B0000000) or not?
I have completed a small project I did to challenge myself to learn more about my arduinos programming. In this project, inside a for loop I have to check whether a MyVar byte fetched from an array of bytes, is "empty" (or `0`, or `B0000000` what applies best). Rough description of the program in note 1.

I did a lot of trial and error, and quickly realized that just asking

```
byte MyVar = B0001111
if {MyVar = 0 ; DoSomethingSpecial() } else {ProceedLoop()}

```

did not produce anything near the desired state. I deduced that testing bytes probably need to happen bitwise, or something clever (=beyond my understanding) is going on in how the compiler interprets the conversion.

So I created a dummy variable byte

```
byte EmptyValue = B0000000

```

and then did a logical `AND` between the MyVar and the EmptyValue

```
if (MyVar&EmptyValue) {DoSomethingSpecial(); } else {ProceedLoop(); }

```

which *seemed* to do the trick - it was getting late and my ability to find useful Serial.print() to capture the different values was getting low, but the results running the program were at the time indistinguishable to me from the expected way.

My question, firstly - is this a reasonable way to check if a byte is zero? Does it actually do that? I have been reading up on these bitwise calculations and I am truly confused to how it compares a final `TRUE` or `FALSE` from comparing.

Secondly, what would be a more processing-time-optimal way of doing this comparison? Is there a null-test of sorts? Should I have cast it into integer and tested against integer 0?

---

```
  const int interval=400;
  const byte ByteArrayMakeSound[11]={
    B0001111,                                       //H - length 4
    B0001111,                                       //E - length 4
    B0001111,                                       //L - length 4
    B0001111,                                       //L - length 4
    B0000111,                                       //O - length 3
    B0000000,                                       //
    B0000111,                                       //W - length 3
    B0000111,                                       //O - length 3
    B0000111,                                       //R - length 3
    B0001111,                                       //L - length 4
    B0000111                                        //D - length 3
    };
  const byte ByteArrayLongShort[11] =   {
    B0001111,                                       //H = --- --- --- ---
    B0000001,                                       //E = o o o ---
    B0001011,                                       //L = --- o --- ---
    B0001011,                                       //L = --- o --- ---
    B0000000,                                       //O = o o o
    B0000000,                                       // (pause 7 intervals)
    B0000100,                                       //W = --- o o
    B0000000,                                       //O = o o o
    B0000101,                                       //R = --- o ---
    B0001011,                                       //L = --- o --- ---
    B0000011                                        //D = o --- ---
  };

void setup()
{
  pinMode (LED_BUILTIN, OUTPUT);
}

void loop()
{
  for (byte x = 0; x < 10; ++x) {
   byte sound = ByteArrayMakeSound[x];
   byte longshort = ByteArrayLongShort[x];
   byte emptyvalue {B0000000};
    if (sound&emptyvalue) {
      delay(7 * interval);
    }    else    {
      for (byte y = 0; y < 7; ++y)  {
        if (sound & (1<<y)){
          if (longshort & (1<<y))     {
            MakeSound(3);
            delay(interval);
          }   else    {
            MakeSound(1);
            delay(interval);
          }
        }   else      {}
      }
    }
      delay(3 * interval);
  }
}

void MakeSound(int del) {
  digitalWrite(LED_BUILTIN, HIGH);
  delay(interval * del);
  digitalWrite(LED_BUILTIN, LOW);
}

```

---

**Note 1: Program purpose**

> The program started as a hello world program with me trying to write "hello world" in morse code with the builtin led. The two byte arrays store the "letter length" of the morse and the "daa" / "dit" assignment. I use empty bytes to separate words, which means a delay of 3 intervals (= length of "daa") should happen. Then including the entire letter-table of morse and a string to byte table conversion (to create the byte arrays). It is not very quick... But I am tinkering with moving the byte arrays into the flash memory. Just for Hello World it is fast enough, but the "daas" and the "dits" are slightly uneven so programming time is impacting performance.

# Answer
> 4 votes
The fast way is:

```
byte x;
...
if (x) {
// not zero.
} else {
// zero
}

```

You are barking at the wrong tree.

---
Tags: programming
---


# Question
Title: Do I Have to Change Internal Interrupt Setups when I Change Clock Speed?
Do I Have to Change Internal Interrupt Setups when I Change Clock Speed? The reason I ask is because I am trying to modify this code:

https://github.com/DccPlusPlus/BaseStation/blob/master/DCCpp\_Uno/DCCpp\_Uno.ino (Beginning at Line 289)

so that if will work on an ATmega328P-derivative board running at 3.3V and 8 MHz. I need the waveforms to be generated at the same speed, so is there anything I have to change? The original code is written for a 16 MHz uno or mega.

# Answer
The equation for timer period is given by the following formula, where `N` is the value of the prescaler, `TOP` is the value given by either `MAX` (i.e. `0xFF`, `0xFFFF`) or the value in register `OCRnx`, and `f` is the frequency of the I/O clock.

If you're using phase-correct or phase- and frequency-correct mode, the factor (`TOP`+1) in the formula above should be replaced by 2 × `TOP`.
> 1 votes

---
Tags: interrupt, atmega328, arduino-pro-mini, oscillator-clock
---


title: How to get HTTPS on Arduino?

text:
# Question
Title: How to get HTTPS on Arduino?
Put plainly: is there a way to get an HTTPS connection on the Arduino?

I have been looking in to it, and I have found it is impossible with the standard library and the Ethernet shield, but is there a custom library that can do it?

What about a coprocessor, i.e. like the WiFi shield has? Anyone know if the Arduino yún has ssl?

# Answer
> 14 votes
MCUs mounted on Arduinos don't have the horse power to handle https connections.

Yún does handle https **on the linux side**, with software like curl, wget or python. Your sketch can just delegate the task to the linux side.

While curl works fine, despite having python preinstalled, you need to manually install python-openssl, since it's not available out of the box (due to disk space constraints)

# Answer
> 12 votes
<sup>(Disclaimer: I'm actually the author of the evothings.com article)</sup>
The Arduino UNO is more than capable at doing TLS. I've just finished up a blog post series on the topic. I have a working example of an Arduino UNO doing elliptic curve cryptography (sect163r2) to exchange an AES-128 key for continued communication.

http://ardiri.com/blog/utls\_defining\_lightweight\_security\_for\_iot\_part\_1
...
http://ardiri.com/blog/utls\_defining\_lightweight\_security\_for\_iot\_part\_8

I took an alternative approach to trying to make a true HTTPS connection - instead, this is using the same protocols under the hood but using an insecure communication channel to do so. Pretty much TLS on HTTP.

# Answer
> 8 votes
I list this as an answer for the sake of having it linked here. It is not full https yet but he is working on it and it may be doable in the future http://evothings.com/is-it-possible-to-secure-micro-controllers-used-within-iot/

here is a benchmark he did encrypting a message using a 1024 bit public key

```
Arduino UNO       16Mhz AVR               ==> 12596 ms*   8504 ms#
Arduino Leonardo  16Mhz AVR               ==> 12682 ms*   8563 ms#
Arduino Mega      16Mhz AVR               ==> 12596 ms*   8504 ms#
Arduino Due       84Mhz ARM               ==>  1032 ms*
Arduino Yún       16Mhz AVR + 400Mhz MIPS ==>   707 ms*
Intel Galileo     400Mhz x86              ==>   192 ms*

```

This is as far as he has gone with it but is in the process of setting up a test server to work on seeing how close to a full ssl/https implementation he can pull off.

# Answer
> 6 votes
I don't think it is possible due to the size and complexity of the SSL Library, because the Arduino would most likely be under powered. That being said you could make the requests to a regular server and use some sort of PHP script to proxy the request to the HTTPS server. Not sure how well it would work though.

You may be interested into looking at this.

# Answer
> 1 votes
As Federico Fissore says above, the Yún can only handle HTTPS (or SSL, whatever you want to call it) on the Linux side of the Yún.

The first way to do it is using Python with Python OpenSSL. The way you do this is by issuing the following commands via SSH or YunSerialTerminal:

```
opkg update
opkg install python-openssl

```

`opkg update` will make sure the package list is up-to-date, and then `opkg install` installs Python OpenSSL. And then you can talk to the Arduino using Python. This page on the Arduino website should help you in using Python with Arduino.

You could also use `curl` with the `-k` option in your sketch. For example:

```
Process process;
process.runShellCommand("curl -k http://example.net");
while(p.running()); // this waits for the command to be done before continuing

```

Also, if you want to download a file, you could use `wget`. This would require you to upgrade `wget` by again issuing these commands via SSH or YunSerialTerminal:

```
opkg update
opkg upgrade wget

```

And then you can just do this in your sketch:

```
Process process;
process.runShellCommand("wget http://example.net");
while(p.running()); // this waits for the command to be done before continuing

```

# Answer
> 1 votes
Using of WiFi shield can be a solution - it supports HTTPS.

https://www.arduino.cc/en/Reference/WiFi101

---
Tags: ethernet, arduino-yun, web
---


# Question
Title: Formula for decision making when in contact with an obstacle using Arduino
I am currently working on an obstacle avoidance robot project and I have seen numerous algorithms for obstacle avoidance a very good example is this Obstacle avoidance algorithm \[Editor's note: by user3880651\], but unfortunately non of those algorithm works with Arduino so upon my research I found this blog

and in there the author talks about an effective way to avoid obstacles and even has a formula he uses for avoiding obstacles at different positions e.g (Front and sides) but doesn't really explain how he got the formula so I was wondering if anyone as an effective formula or algorithm for obstacle avoidance that works with Arduino thanks for your help in advance

# Answer
> 2 votes
The algorithm mentioned in the blog uses a Fuzzy Logic Controller to perform obstacle avoidance. You can read up more on fuzzy logic here: https://www.calvin.edu/~pribeiro/othrlnks/Fuzzy/home.htm

> newMotorSPD\_L = basicVelocity + urgMotor\_L + (urgTurn\_R/4) - (urgTurn\_L/2) + 60; newMotorSPD\_R = basicVelocity + urgMotor\_R + (urgTurn\_L/4) - (urgTurn\_R/2) + 60;

Based on the code, the formula is basically made up of variables with different weight/ratio.

**basicVelocity** is the default moving velocity of the robot when there is no obstacle or wall detected.

**urgMotor\_L/urgMotor\_R** is the ouput of the controller for frontal object detection/avoidance by getting the difference between the left and right ultrasonic sensor.

**(urgTurn\_R/4) - (urgTurn\_L/2)** is probably the left/right bias?

**+60** at the end is probably to offset the error of the motor. (Assuming all other variable are 0, the robot should be still at 60 for their case?)

To simplify what the formula does, it can be shown using this graph:

# Answer
> 0 votes
Obstacle avoidance doesn't really have any practical use unless it works alongside a goal finding algorithm.

Take a look here for a simple introduction (and a few modifications) to this excellent paper on goal finding with obstacle avoidance.

You can also experiment with the python code (add obstacles, change positions of goal and start points, and step through the decision process). Keep in mind that in this code, ***sensor inputs are also simulated*** and so the code is much more complex than that would actually need to be deployed on a robot with real world sensors.

---
Tags: arduino-uno, arduino-mega, programming, arduino-ide, c
---


# Question
Title: Is there any arduino microphone that can record heart sound?
I'm currently undergoing a project where I record heart sound (mechanical response) and send it over bluetooth to an app.

However I'm finding it really hard to find an arduino microphone or just general microphone that can listen to heart sound.

Any guide would be highly appreciated.

Thanks

# Answer
> 1 votes
First, there is no such thing as an Arduino microphone. There are boards specifically for Arduino which have a microphone, but also these are just generic 'electronic sound sensors'.

In principle any microphone would be a valid choice, but you have to test what are the requirements (distance to heart, environment noice). You also could use piezo sensors for vibration checking possibly.

---
Tags: arduino-uno, sound
---


# Question
Title: Help with multi function momentary buttons
I'm using the PunchThrough Bean and trying to build a two button project that has multiple functions. The idea is the user can press button 1 or button 2 for either short or long presses, or they can press both buttons simultaneously for a short or long press. This would give the user up to 6 different functions. Below is my code. I can get button 1 or button 2 press to work, but can't get pressing both to work. Also, I can't figure out how to have a function depending on how long a button sequence was pushed. Any help would be greatly appreciated.

```
const int btn0 = 2;
const int btn1 = 3;

void setup() {
  Bean.enableWakeOnConnect(true);
  Bean.enablePairingPin(true);
  BeanHid.enable();
  pinMode(btn0, INPUT_PULLUP);
  pinMode(btn1, INPUT_PULLUP);
}

void loop() {
  bool connected = Bean.getConnectionState();
  pinMode(btn0, INPUT_PULLUP);
  pinMode(btn1, INPUT_PULLUP);

  if (connected) {
    int btn0State = digitalRead(btn0);
    int btn1State = digitalRead(btn1);

    if ((btn0State == LOW) && (btn1State == HIGH)) {
      Bean.setLed(255, 0, 0);
      Serial.print("up");
      BeanHid.sendKey(KEY_UP_ARROW);
      Bean.sleep(500);
      Bean.setLed(0, 0, 0);
    }

    if ((btn1State == LOW) && (btn0State == HIGH)) {
      Bean.setLed(0, 255, 0);
      Serial.print("down");
      BeanHid.sendKey(KEY_DOWN_ARROW);
      Bean.sleep(500);
      Bean.setLed(0, 0, 0);
    }

    if ((btn1State == LOW) && (btn0State == LOW)) {
      Bean.setLed(0, 0, 255);
      Serial.print("right");
      BeanHid.sendKey(KEY_RIGHT_ARROW);
      Bean.sleep(500);
      Bean.setLed(0, 0, 0);
    }

  }
}

```

# Answer
Don't act immediately when a button gets pressed, instead when a button is pushed all you do is set the timestamp on that button and set a flag.

Then when it is released or the time elapsed is large enough do the appropriate thing.

```
void loop(){
    int btn1State = digitalRead(btn1);
    static bool button1 = false;
    static unsigned long time1 = 0;

    if (btn0State == LOW && !button1) {
        button1 = true;
        time1 = millis();
    }

    if (btn0State == LOW && button1) {
        if(millis() - time1  > SHORT){
            //do long action
            time1 = millis(); //reset timer
        }
    }

    if (btn0State == HIGH && button1) {
        button1 = false;
        //do short action
    }

}

```
> 2 votes

---
Tags: programming, button
---


# Question
Title: What's the point of a USB to Serial converter in Arduino Uno?
I am trying to understand how an Arduino Uno communicates with a PC and why it does so.

As far as I understand, an Arduino can be programmed to communicate with a PC only via a COM interface. At the same time, an Arduino Uno uses a physical USB interface to connect to a PC and it has a USB-serial converter onboard. So data sent from the board is converted from COM to USB to be transferred to the PC where USB data is converted to COM by a device driver to be read by a COM-port listener.

Why does this COM-related stuff is needed if the Arduino already uses the USB stack for connection? And is the Arduino able to emulate a keyboard and mouse if it has to use interrupts for this, so it has to act like USB device without using COM?

# Answer
The problem with your understanding is with what "COM" is.

"COMx" is not a physical serial port - it is merely a concept. It is a "serial device" in Windows.

That Serial Device is implemented as a CDC/ACM protocol on USB. It's not "converted" - "COMx" is just the visible interface to the PC's end of the CDC/ACM connection.

The ATMega328P chip (and the ATMega2560) can't talk USB directly. They talk UART. So UART data is sent to another chip that can talk both USB and UART to bridge the two technologies.

So from the PC perspective the chain is:

```
CDC/ACM Driver ("COMx") => USB => ATMega16U2 => UART => ATMega328P

```

Boards with an ATMega32U4 chip on them, like the Leonardo, can talk USB directly - so the sequence is much shorter:

```
CDC/ACM Driver ("COMx") => USB -> ATMega32U4

```

Again there is *no* conversion from "COMx" to "USB" since "COM" is just the name given to the exposed end of the CDC/ACM driver.

It can be said that CDC/ACM *emulates* a serial port over USB. It forms a channel between the CDC/ACM driver ("COMx") and a pair of *bulk* endpoints in the chip at the other end of the connection.
> 5 votes

---
Tags: arduino-uno
---


title: Arduino only supports 140 LEDs

text:
# Question
Title: Arduino only supports 140 LEDs
I've a problem with an Arduino Uno board. It only supports 140 LEDs using the FastLED library. Any LED which is higher than that number can't be turned on or off using FastLED.

Is this related to the board, the library or any other circumstances? And help or advice would be appreciated.

# Answer
> 2 votes
Depending on how fast you need to go, the apa102-arduino library by Pololu doesn't seem to have a hard limit on their LED capacity. They say the refresh rate goes down after 60 LEDs, but don't say anything about the maximum amount of LEDs you can have.

https://github.com/pololu/apa102-arduino

The max I'm doing at a time is only a string of 48, so I haven't checked how many it can update at once. I am, however, running a total of 180 LEDs with this library, over 5 separate "channels".

To make it even a tiny bit harder, I'm doing it with a Nano.

This library has a "slow" and "fast" way to update the LEDs. So far, the "slow" way is faster than the BlueTooth connection I'm sending data over to update the LEDs.

They state that the "fast" method is something like 20x faster than the "slow" way, but takes a little more in-depth programming. Since the "slow" way works for me, so far, I haven't bothered trying the "fast" way.

Other option:

Talking with a friend at my local maker space, he said that it could be due to the speed of your board vs the timing of your LEDs. If your board isn't running fast enough, it can prevent the LEDs from being updated correctly after a certain amount of LEDs.

He was also saying that if you don't have enough available memory, it can affect how many LEDs you can update.

Seeing as you have an Arduino Uno, you shouldn't have a problem with speed or memory. Without knowing what else you are running on your Arduino (code, sensors, GPS, other shields, etc.), it's hard to say what could be holding you back.

My friend's son uses the FastLED library, so I'm going off other-than-first hand knowledge here.

---
Tags: arduino-uno
---


title: Interrupts not firing sometimes

text:
# Question
Title: Interrupts not firing sometimes
I am a little new to the arduino space, so forgive me if this question is a bit noobish.

Below I have linked the code. I am using an Arduino Mega and using photosensors, specifically this one EE-SPY415 Reflective Photomicrosensor with a detectable sensing distance of 3- to 19-mm

I am using this with a skeeball type arcade and the Arduino is using usb to communicate with a Pi. I am not sure if this is the best way, so if someone has a better way please let me know. There are 6 sensors now and 2 more that need to be hooked up. I will be using a library that allows for more than 6 interrupts to be used, but wanted to rule out the library as being at fault.

My issue in question is when the game is started, the serial.write isn't always firing when a ball goes through the hole. I am not sure yet if the interrupt fires and not the serial write, so any help there would be good too.

https://pastebin.com/zbVuqec2

Here is the ino file in question.

```
#include <Wire.h>

String inputString = "";         // a string to hold incoming data
boolean startGame = false;
boolean stopGame = false;
boolean readSensors = false;

boolean sensor_1_tripped = false;
boolean sensor_2_tripped = false;
boolean sensor_3_tripped = false;
boolean sensor_4_tripped = false;
boolean sensor_5_tripped = false;
boolean sensor_6_tripped = false;

#define SENSOR_1 2
#define SENSOR_2 3
#define SENSOR_3 21
#define SENSOR_4 20
#define SENSOR_5 19
#define SENSOR_6 18

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  pinMode(SENSOR_1, INPUT_PULLUP);
  pinMode(SENSOR_2, INPUT_PULLUP);
  pinMode(SENSOR_3, INPUT_PULLUP);
  pinMode(SENSOR_4, INPUT_PULLUP);
  pinMode(SENSOR_5, INPUT_PULLUP);
  pinMode(SENSOR_6, INPUT_PULLUP);

  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

void loop() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inputString == "start") {
      startGame = true;
      stopGame = false;
      readSensors = true;
      Serial.write("STARTED");
      inputString = "";
      return;
    }
    if (inputString == "stop") {
      stopGame = true;
      startGame = false;
      readSensors = false;
      return;
    }
  }

  while (startGame) {
    while(readSensors) {

      attachInterrupt(digitalPinToInterrupt(SENSOR_1), sensor_1_callback, FALLING);
      attachInterrupt(digitalPinToInterrupt(SENSOR_2), sensor_2_callback, FALLING);
      attachInterrupt(digitalPinToInterrupt(SENSOR_3), sensor_3_callback, FALLING);
      attachInterrupt(digitalPinToInterrupt(SENSOR_4), sensor_4_callback, FALLING);
      attachInterrupt(digitalPinToInterrupt(SENSOR_5), sensor_5_callback, FALLING);
      attachInterrupt(digitalPinToInterrupt(SENSOR_6), sensor_6_callback, FALLING);

      if(Serial.available()) {
        char inChar = (char)Serial.read();
        inputString += inChar;
        if (inputString == "stop") {
          stopGame = true;
          startGame = false;
          readSensors = false;
          inputString = "";
          Serial.write("STOPPED");
          break;
        }
      }

      if(sensor_1_tripped == true) {
        sensor_1_tripped = false;
        Serial.write("1");
      }
      if(sensor_2_tripped == true) {
        sensor_2_tripped = false;
        Serial.write("2");
      }
      if(sensor_3_tripped == true) {
        sensor_3_tripped = false;
        Serial.write("3");
      }
      if(sensor_4_tripped == true) {
        sensor_4_tripped = false;
        Serial.write("4");
      }
      if(sensor_5_tripped == true) {
        sensor_5_tripped = false;
        Serial.write("5");
      }
      if(sensor_6_tripped == true) {
        sensor_6_tripped = false;
        Serial.write("6");
      }
    }
  }
}

void sensor_1_callback() {
  sensor_1_tripped = true;
}
void sensor_2_callback() {
  sensor_2_tripped = true;
}
void sensor_3_callback() {
  sensor_3_tripped = true;
}
void sensor_4_callback() {
  sensor_4_tripped = true;
}
void sensor_5_callback() {
  sensor_5_tripped = true;
}
void sensor_6_callback() {
  sensor_6_tripped = true;
}

```

# Answer
> -1 votes
The best solution I found for communicating interrupts to the Pi3 from Arduino is by using the GPIO. Android Things has a library for this and it all works without any lag!

---
Tags: arduino-mega, interrupt
---


# Question
Title: Releasing gas from a bottle via Arduino - opening a valve?
I'm looking for a mechanical component that enables me to do two things:

* contain a gas in a bottle and if opened, the gas should smoothly flow out (with the same pressure as a child blowing out candles on his birthday cake)
* a valve for the bottle that can be opened by an Arduino when powered via USB

I already figured out that there are outlets for CO2 capsules. This could work, but then I'd need to figure out a way to decrease the pressure in order to receive a gentle gas flow.

Do you know any better approaches? Thanks in advance!

# Answer
> 1 votes
If you can tolerate a decreasing flow as the pressure in the bottle drops, a simple restrictor (fluidic equivalent of a resistor) will limit the flow but of course, I = E/R; the flow will be proportional to the bottle pressure. If that is acceptable, it is probably the cheapest, simplest, and most reliable way.

# Answer
> 0 votes
You can use the product "electronic expansion valve" to control gas, also you can control valve openness degree this is optional, then put in CO2 capsules closed box also add air pressure sensor in the box, air pressure sensor controls valve to set up gas pressure, make one hole to box the gas out from here thereafter measure the pressure what you want. Also do you need a mechanic component to control CO2 capsule?

---
Tags: power, components
---


# Question
Title: How to send message from one embedded system to another embedded system like in cars, from one car to another?
I want to send message from one embedded system to another embedded system, like from one car to another without using phone number. just turn on the wifi.. connect and send message. There are ways to to send text to phone but not this kind of texting? Iam short of time. I only need to know what will be required to do this ? PLease help

# Answer
> 1 votes
You can use ESP8266 modules( like Wemos d1) for wifi connections. The good thing about those are that they are also programmable and you can use arduino IDE to program. You can get two of those and make first one a server/host and second on a client for connection. There is more options of course: Bluetooth, RF link etc. Google "short distance connection arduino"

# Answer
> 1 votes
1. **ESP8266** WiFi Module is a WiFi serial transceiver module, based on ESP8266 SoC. This chip implements a full TCP/IP protocol stack and the very interesting feature is that it has also a great computational power onboard. You can easily program it using Arduino IDE. You can buy this development boards in town market and its very low cost also.
2. **Bluetooth/BLE4.0** Bluetooth is a type of wireless communication used to transmit voice and data at high speeds using radio waves. It is a standard protocol for short-range radio communications between many different types of embedded devices. Devices need to be within approximately 10 meters of each other, and the typical data transfer rate is around 2 megabits per second (Mbps).

These two are the most preferable for shorter distance communication.

---
Tags: arduino-uno, c, arduino-micro
---


# Question
Title: Advice for lower power clock type project
Arduino newbie here! I'm a programmer but my hardware knowledge is ... slim.

I'd like to build a clock type wake up timer for my daughter, using an Arduino type board. Probably overkill, but it's practice for me, not just the end result! The idea is that is sleeps all night, then around say 4, gradually fade on a red LED. At 6, which is when she's allowed to come in, the red LED switches to a green, or some colourful sequence that tells her it's time. At a later time, maybe 7/8, it goes back to sleep to conserve power.

I have an Arduino Uno, but looks like I'll also need an RTC, and I've read that the sleep power consumption is ~40mA, which on batteries is what? A day or two max?

So I'm thinking I can't do this with a Uno.

So, my question is, are there any very low power Arduino compatible boards with built in RTCs that I can use for this project that will get me up and running as quickly as if I was using the Uno?

# Answer
I will make my comment an answer:
Try one of Arduino supported RTC's boards. The most popular (and simple) are: DS1302, DS1307, PCF8563, and - very nice, precise DS3231 (my fav). Hook any of them up to your Arduino, add some (readily available) libraries, tweak a little and voila. The RTC will work from coin battery (or high capacity capacitor called supercapacitor, like this one)

Most (if not all) of mentioned RTC have 'alarm' function: you can set up your system to wake up your Arduino from sleep (sleep state to save battery) and gradually light up the LED.

To limit current of Arduino try removing LED, or - better - try to use Arduino Pro Mini. To modify it for better power saving see here

EDIT:
There are some other low-power boards:
Moteino
Rocket Scream Mini Ultra
JeeLink

Also, (as mentioned in one of comments) you can try one of ESP8266 and push it to one of many sleep modes. Try this link.
> 1 votes

---
Tags: arduino-uno, rtc
---


# Question
Title: Updating firmware on Adafruit Huzzah ESP8266
**tldr**: I'm trying to re-flash a previously working Adafruit Huzzah ESP8266 breakout module to use the latest AT firmware. The re-flash doesn't seem to be working and I'm getting rather confused. This is the first time I've re-flashed one of these modules.

**What I did**:

I installed the latest version of esptool.py using pip, and downloaded the latest Espressif AT firmware from github. With the Huzzah on a breadboard I grounded GPIO0 and connected the serial port header to my PC using a FTDI cable. VC on the cable is 5v. The red power light lit. I then ran the following commands:

> esptool.py --port COM4 --baud 115200 write\_flash -fm dio -fs 32m 0x00000 boot\_v1.1.bin
>
> esptool.py --port COM4 --baud 115200 write\_flash -fm dio -fs 32m 0x01000 user1.bin
>
> esptool.py --port COM4 --baud 115200 write\_flash -fm dio -fs 32m 0x7C000 esp\_init\_data\_default.bin
>
> esptool.py --port COM4 --baud 115200 write\_flash -fm dio -fs 32m 0x7E000 blank.bin

Each upload seemed to complete ok, but I found that I couldn't upload the next until I pressed the reset button on the module first, otherwise I got the error:

> A fatal error occurred: Failed to connect to Espressif device: Timed out waiting for packet header

This doesn't seem right to me.

After uploading all 4 components as above I disconnected GPIO0 and cycled the power on the module. I then tried connecting using putty at 115200 baud but I get no echo back from the module, and it doesn't respond to commands.

Observations:

* The module previously worked and I could connect to it with putty.
* If I reset it while connected to putty at 115200, I get a consistent pattern of garbage characters, so my guess is its alive but confused somehow.
* I've tried connecting at various baud rates: 9600, 14400, 19200, 38400, 57600, as well as 115200. Same result.
* I've found walkthroughs that show firmware like node.mcu being uploaded as a single binary. Should I be somehow merging the four bin files into one before uploading the the Huzzah?

What am I doing wrong here?

# Answer
I'm confident that I've resolved this now. I'll leave the question here in case it is useful to anyone in the future.

The problem was they way I was downloading the individual bin files from the github repo, using right click -\> Save As in Chrome. This downloads a file with the correct name, but it is actually the content of the file's html page, not the binary itself. Downloading the repo as a zip file, extracting the bin files, and then uploading them to the Huzzah using a single command gave me a properly flashed device.

What happened was I noticed that esptool can upload multiple files in one go so, with the original bin files, I tried the command:

> esptool.py --port COM4 --baud 115200 write\_flash -fm dio -fs 32m 0x00000 boot\_v1.1.bin 0x01000 user1.bin 0x7C000 esp\_init\_data\_default.bin 0x7E000 blank.bin

and got the following error

> esptool write\_flash: error: argument : Detected overlap at address: 0x1000 for file: user1.bin

Thats when I noticed that boot\_v1.1.bin was way to big to fit in the space between 0x00000 and 0x01000. Examining it in notepad++ revealed that it was html.

A learning experience...
> 2 votes

---
Tags: esp8266, adafruit
---


title: Max length of char arrays?

text:
# Question
Title: Max length of char arrays?
I'm running the following Sketch on my Arduino Uno:

```
const boolean verbose = true;

void debug(const char *s){
    if(verbose){
      char buf[8 + sizeof(s)] = "[DEBUG] ";
      strcat(buf, s);
      Serial.println(buf);
   }
}

void setup() {
  Serial.begin(115200);
  char str[] = "Lorem ipsum dolor sit amet, consetetur sad";

  Serial.print("Size of String: ");
  Serial.println(sizeof(str));
  debug(str);
}

void loop() {
}

```

The `debug()` function should be used to print debugging information to Serial. The Sketch like I posted it works wuite fine and prints the following to Serial: Size of String: 43 \[DEBUG\] Lorem ipsum dolor sit amet, consetetur sad

But when I add one more character to the string (43 chars excluding null terminator) I get the only follwoing on Serial:

```
Size

```

This problem does not appaer when defining `str` as a global varaible. What does cause this issue and how can it be fixed?

# Answer
> 6 votes
The main issue with your code is:

```
sizeof(s)

```

That doesn't return the string length - it returns the size of the char pointer - which is two bytes (on an 8-bit system, 4 on a 32-bit system).

Instead you need to use `strlen(s)` and add one to it for the NULL character at the end.

```
if(verbose){
  char buf[8 + strlen(s) + 1] = "[DEBUG] ";
  strcat(buf, s);
  Serial.println(buf);
}

```

However, what you are doing is pointless. Just print twice:

```
if(verbose){
  Serial.print(F("[DEBUG] "));
  Serial.println(s);
}

```

Note the use of the `F()` macro to ensure that `[DEBUG]` doesn't waste RAM.

# Answer
> 1 votes
`sizeof(s)` does not do what you thing it does. All it does is take the sizeof of the pointer.

So you end up with Undefined Behavior when you concatenate the strings which means than *anything* is allowed to happen.

Instead you can do 2 print statements to concat on the serial stream:

```
void debug(const char *s){
    if(verbose){
      Serial.print(F("[DEBUG] "));
      Serial.println(s);
   }
}

```

---
Tags: arduino-uno, programming, c, array, c-string
---


title: Error AT%13% With GSM/GPRS Shield

text:
# Question
Title: Error AT%13% With GSM/GPRS Shield
I've been trying to control a relay with a GSM shield for an Arduino project. I haven't had any success with either the Software Serial or the GSM library. I've declared the GSM variable in debug mode and it gives me the error AT%13%. Using Software Serial, it hangs at SIM900.read(). The SIM card connects to the network, I can see that on the board - the LED flashes every 3 seconds and gives me signals for incoming calls and SMSs.

I can't tell if the error is with the SIM card, the connections to the Arduino board, the power supply (12V) or something completely different. Here are the two code examples I'm using with the different libraries.

Anyone else getting this error? What is the best way to make it go away?

Thanks in advance for any help!

**SoftwareSerial.h**

```
#include <SoftwareSerial.h>
char inchar; // Will hold the incoming character from the GSM shield
SoftwareSerial SIM900(7, 8);

int Relay1 = A2;
int Relay2 = A3;

void setup()
{
  Serial.begin(19200);
  //Set up relay control pins
  pinMode(Relay1, OUTPUT);
  digitalWrite(Relay1, HIGH);
  // wake up the GSM shield
  SIM900.begin(19200);
  delay(10000);  // give time to log on to network.
  SIM900.print("AT+CMGF=1\r");  // set SMS mode to text
  delay(100);
  SIM900.print("AT+CNMI=2,2,0,0,0\r");
  // output contents of new SMS upon receipt to the GSM shield's serial out
  delay(100);
  Serial.println("Ready...");
  }

void loop()
{

  //If a character comes in from the cellular module...
  if(SIM900.available() >0)
  {
    inchar=SIM900.read();
    Serial.println(inchar);
    if (inchar=='#')
    {...}
    SIM900.println("AT+CMGD=1,20"); // delete all SMS
  }
}

```

**GSM.h** \- in this case I haven't even gotten past the .begin() command. This code is from the example at http://arduino.cc/en/Tutorial/GSMExamplesReceiveSMS

```
#include <GSM.h>

// PIN Number for the SIM
#define PINNUMBER ""

// initialize the library instances
GSM gsmAccess(true);
GSM_SMS sms;
GSM3_NetworkStatus_t   gsmStatus;
// Array to hold the number a SMS is retreived from
char senderNumber[20];

void setup()
{
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  Serial.println("SMS Messages Receiver");
  char code = 'X';
  Serial.flush();
  gsmStatus = gsmAccess.begin(PINNUMBER,false,false);

  int retrycount = 60;
  while (gsmStatus != GSM_READY && retrycount-- > 0) {
    delay(1000);  // lower delay value could lead to unstable state
    theGSM3ShieldV1ModemCore.manageReceivedData();
    gsmStatus = gsmAccess.getStatus();
  }

  if (gsmStatus != GSM_READY) {
    Serial.println("\n[ERR]: GSM connection failed\n");
    Serial.flush();
    gsmAccess.shutdown();
    delay(250);
    digitalWrite(3,LOW);
  }

  Serial.println(" [DONE]");

  // connection state
  boolean notConnected = true;
  // Start GSM connection
  while (notConnected)
  {
    if (gsmAccess.begin(PINNUMBER) == GSM_READY)
  {
      notConnected = false;
  }
    else
    {
      Serial.println("\nNot connected");
      delay(1000);
    }
  }
  Serial.println("GSM initialized");
  Serial.println("Waiting for messages");
}

```

# Answer
Check your baud rates. They appear to be different. That will cause problems.
> 1 votes

# Answer
I used Mega 2560 connected to GSM shield as following:

* GSM TX --- pin 10 on Mega
* GSM RX --- pin 3 on Mega
* GND to GND
> 1 votes

---
Tags: arduino-uno, shields, gsm
---


title: MCP4725 not working

text:
# Question
Title: MCP4725 not working
I tried using a MCP4725 DAC with an Arduino (M0 Pro, Duemilanove and Diecimila) and it is not outputing any voltage (just random noise).

I've been using the 2 tutorial codes fount here : https://learn.adafruit.com/mcp4725-12-bit-dac-tutorial/using-with-arduino which are supposed to generate a sinus wave and a triangle wave.

I am pretty sure it doesn't come from the Arduino because basic examples have been working so far.

Anybody had the same issue or can help on this ?

```
#include <Wire.h>
#include <Adafruit_MCP4725.h>

Adafruit_MCP4725 dac;

void setup(void) {
  Serial.begin(9600);
  Serial.println("Hello!");

// For Adafruit MCP4725A1 the address is 0x62 (default) or 0x63 (ADDR pin tied to VCC)
  // For MCP4725A0 the address is 0x60 or 0x61
  // For MCP4725A2 the address is 0x64 or 0x65
  dac.begin(0x62);

  Serial.println("Generating a triangle wave");
}

void loop(void) {
    uint32_t counter;
    // Run through the full 12-bit scale for a triangle wave
    for (counter = 0; counter < 4095; counter++)
    {
      dac.setVoltage(counter, false);
    }
    for (counter = 4095; counter > 0; counter--)
    {
      dac.setVoltage(counter, false);
    }
}

```

# Answer
I finally got this example to work.

Details:

My A0 line was tied to ground using the solder jumper (through a 10k SMD resistor on the board it appears). I used . . .

# define MCP4725\_ADDR 0x62

. . . as the base address of the chip. My CMD to update the DAC was . . .

Wire.write(64);

. . . (64 in decimal i.e. 0b01000000)

This YouTube video helped out a lot: https://www.youtube.com/watch?v=ngvqgIeaS9M

All the best KenLaszlo
> 1 votes

---
Tags: arduino-mega
---


# Question
Title: I can not upload hex files outside of the Arduino IDE to my Pro Micro
I have a strange problem. I am trying to upload a .hex file to my lovely Arduino Pro Micro, but am unable to do so. If I use the Arduino IDE, it goes through perfectly fine and uploads the sketch. However, if I try to use avrdude from the terminal I get:

```
avrdude: butterfly_recv(): programmer is not responding
avrdude: error: programmer did not respond to command: leave prog mode
avrdude: butterfly_recv(): programmer is not responding
avrdude: error: programmer did not respond to command: exit bootloader

```

My commandline is:

```
avrdude -patmega32u4 -cavr109 -P/dev/ttyACM0 -t

```

and the full output is:

```
Connecting to programmer: .avrdude: butterfly_recv(): programmer is not responding

avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
Found programmer: Id = ""; type =
    Software Version = .; Hardware Version = .
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: error: buffered memory access not supported. Maybe it isn't
a butterfly/AVR109 but a AVR910 device?
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.

Connecting to programmer: .avrdude: butterfly_recv(): programmer is not responding

avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
Found programmer: Id = ""; type =
Software Version = .; Hardware Version = .
avrdude: butterfly_recv(): programmer is not responding
avrdude: butterfly_recv(): programmer is not responding
avrdude: error: buffered memory access not supported. Maybe it isn't
a butterfly/AVR109 but a AVR910 device?
avrdude: initialization failed, rc=-1
         Double check connections and try again, or use -F to override
         this check.

avrdude: butterfly_recv(): programmer is not responding
    avrdude: error: programmer did not respond to command: leave prog mode
    avrdude: butterfly_recv(): programmer is not responding
    avrdude: error: programmer did not respond to command: exit bootloader

```

Thank you for any help, it is greatly appreciated.

--Dominic

# Answer
Unlike the 328P and similar boards the 32U4 has no hardware method for triggering a reset to enter the bootloader.

On the boards with a UART -\> USB interface the USB interface chip manually toggles the RESET pin to activate the bootloader. Without that chip a reset can't be done.

So a different method had to be invented. That method involves opening the serial port at 1200 baud and immediately closing it. That triggers a reboot into the bootloader.

The Arduino IDE manually performs that task when you press the upload button. It is not something that avrdude knows how to do.

So you will have to come up with some method of opening the serial port at 1200 baud immediately before running avrdude. Of course, it means the serial interface device disappears for a short while whilst the chip reboots and enters the bootloader, so you need to ensure you have a delay between the reset method and the call to avrdude - a delay that is long enough for your OS to redetect the chip, yet not so long that the bootloader times out.
> 2 votes

# Answer
I had this bizarre problem with my Leonardo and Uno boards. I was even using Arduino IDE (v1.8.5) to upload the code. In some extreme circumstances, one of the boards was not even listed under serial ports (Tools-\>Port)

After many attempts I simply burned bootloader using another Uno, and that solved the issue!

It is still unclear to me what happened but the issue was caused by an RFID card reader using SPI (ICSP pinouts on the board). The card reader (PN532) was sending data to Arduino when either I was uploading the code or the board was booting up.

UPDATE Jan 2020

After time and many attempts, I realised that Arduino UNO should not be accessed via ISP interface during code upload. The module PN532 was randomly massaging UNO when I flushed the microcontroller which corrupted the code.
> 0 votes

---
Tags: usb, uploading, avrdude, arduino-pro-micro
---