# Question
Title: Using the Broadcom AFBR-S50 with Arduino
I'm trying to interface with a Broadcom AFBR-S50MV85G ToF sensor over SPI using an Arduino, which is complicated by the fact that the provided library and reference application is written for Cortex M0-based NXP microcontrollers. The documentation says that implementing the SPI, IRQ, Debug and Timer interfaces would allow use with other platforms, but it doesn't explicitly state which functions require implementations, or if all of them do, and it's not demarcated within the source code either.

The documentation lists the available command words, but no register addresses, which I believe are used by the platform-independent compiled libraries that come with it. This leaves me with no way to interface manually over SPI, meaning that my only choice appears to be to attempt to implement the platform drivers myself, which will probably take me a significant amount of time.

Is there something else I should try before sinking time into something that may not give me results? Has anyone here used this sensor before, and if so, could you point me in the right direction?

P.S.: The documentation I'm referring to is included in the SDK provided in the downloads section of the product page, and I'm not sure I'd be allowed to rehost it somewhere else in order to link it here.

# Answer
I recently looked into this again, and it turns out that Broadcom had released a porting guide for this library in August 2020 (nearly a year after I originally posted this question) that should prove useful to anyone trying to make use of this sensor. It's for the STM32 platform, not Arduino, but it's the most detail Broadcom has provided with regards to this product, and should provide a starting point at least.

I happen to have access to the Nucleo board used in the guide, but I had some issues with following it in some cases due to some differences in the SDK file/directory structure. I did eventually get a passing build out of it, but couldn't get anything out of the sensor itself, which means that I either did something wrong, or had damaged the sensor in my earlier attempts to interface with it; at the time of my last attempt, I was leaning towards the latter. I don't think I'll be revisiting this unit any time soon, so hopefully others will have better luck. And if anyone does get it working, please do report back; I'm curious to know if I'd actually be able to use this thing if I were to buy another one.

P.S: I'm not marking this as answered because a) my question was technically for the Arduino, and b) I haven't been able to confirm that this is working.
> 2 votes

# Answer
I also have been digging for information about the AFBR-S50MV85G and similar sensors for the past week.

Well, I am trying to port it to ATxmegas (ICs that are similar to those ATmegas used in the Arduino Family), but I decided to post my findings here because this is the first forum topic that I have found about this sensor family.

As Joseph mentioned, Broadcom has released a porting guide to Arm devices, but it does not help at all when it comes to porting to other microcontrollers. Additionally, the minimum requirements of this lib according to Broadcom's porting guide are:

> Minimum memory requirements: RAM: 8kByte (4kByte Heap + 4kByte Stack) ROM/Flash: 128 kByte

Because of that and other facts, I also conclude that it is not possible to port the available libraries to 8-bit microcontrollers nor develop a new one due to the lack of description about the SPI register structure.
> 2 votes

---
Tags: sensors, library, spi, ir, firmware
---


# Question
Title: Doest a local struct variable get destroyed?
For a RF project I need to split payload for a max predefined chunk size. For that I created a `RFmsg` struct.

The unexplained part I need help in is why when I create a **local** instance of `RFmsg`, its contents are remain in tact after function is recalled? is it a struct "thing"? or something else I'm missing ?

To solve that I know that wiping struct content after initiating will solve that, but I'm want to be sure.

for that I share a minimal code that:

1. `void newMSG(char *msg)` simulates the splitting part of a long `msg` input.
2. `void printStrcut_content(RFmsg &_payload)` prints struct's content.
3. output show that only after boot, its content are gibberish, but after that, it shows the **content of last struct update**.

***CODE:***

```
#include <Arduino.h>
#define MSG_LEN 10
#define DEVNAME_LEN 8

char *devName = "TEST";

struct RFmsg
{
    int msg_num;
    int tot_msgs;
    int tot_len;
    char payload[MSG_LEN];
    char dev_name[DEVNAME_LEN];
};

void newMSG(char *msg)
{
    RFmsg _payload;
    printStrcut_content(_payload);

    _payload.tot_len = strlen(msg);
    strcpy(_payload.dev_name, devName);
    _payload.tot_msgs = (int)(_payload.tot_len / MSG_LEN);

    if (_payload.tot_len % MSG_LEN > 0)
    {
        _payload.tot_msgs++;
    }

    for (uint8_t i = 0; i < _payload.tot_msgs; i++)
    {
        strcpy(_payload.payload, "");
        for (uint8_t n = 0; n < MSG_LEN; n++)
        {
            _payload.payload[n] = (char)msg[n + i * MSG_LEN];
        }
        _payload.payload[MSG_LEN] = '\0';

        Serial.print("chunk #");
        Serial.print(i);
        Serial.print(": ");
        Serial.println(_payload.payload);
    }
}
void printStrcut_content(RFmsg &_payload)
{
    Serial.print("LEN: ");
    Serial.println(_payload.tot_len);
    Serial.print("PAyLOAD: ");
    Serial.println(_payload.payload);
    Serial.print("name: ");
    Serial.println(_payload.dev_name);
    Serial.print("tot_msgs: ");
    Serial.println(_payload.tot_msgs);
}

void setup()
{
    Serial.begin(115200);
    while (!Serial)
        ;
    Serial.println("\nStart");
}
void loop()
{
    newMSG("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    delay(5000);
}

```

***OUTPUT:***

```
Start
LEN: 36
PAyLOAD: UVWXYZ
name:
tot_msgs: 4
chunk #0: 1234567890
chunk #1: ABCDEFGHIJ
chunk #2: KLMNOPQRST
chunk #3: UVWXYZ
LEN: 36
PAyLOAD: UVWXYZ
name:
tot_msgs: 4
chunk #0: 1234567890
chunk #1: ABCDEFGHIJ
chunk #2: KLMNOPQRST
chunk #3: UVWXYZ
LEN: 36
PAyLOAD: UVWXYZ
name:
tot_msgs: 4
chunk #0: 1234567890
chunk #1: ABCDEFGHIJ
chunk #2: KLMNOPQRST
chunk #3: UVWXYZ

```

# Answer
As described in the comments: Your local struct variable probably always is placed in the same space in memory and this part of the memory isn't overwritten by your program. When declaring local variables they are not implicitly initialized (written with a value). If you don't initialize the variable yourself, its value will be whatever data is currently in that memory space.

The compiler optimizes your code and also decides on where to place variables on the stack. In this case it is always placed at the same memory space, but you cannot count on that. It is undefined behavior and any change to your program can also change that fact. And you don't have any control over that.

This is the reason, why you should always initialize local variables before reading them.

> what do you recommend ?

Just initialize the members of that struct variable before using them. Set all members to a value. You can even do that in one step together with declaring it:

```
RFmsg _payload = {1, 2, 3, "payload", "devname"};

```
> 3 votes

---
Tags: struct
---


# Question
Title: What is the difference between SCLK and CLK pins?
Please do forgive me if anything in this question does not make sense, I am very new to PCB and Arduino.

I am following a tutorial through where the person doing it is making a small data computer (for a balloon or drone, I don't know) on Eagle By Autodesk. I am on a part of the tutorial where an SD & MMC card reader is being used. The instuctor is using a library he has made himself, whilst I am using the default Adafruit library. On his library for the SD card reader, he has a CLK pin, however on the default Adafruit library there is the SCLK pin.

Is there a difference between SCLK pins and CLK pins? And am I ok just to use the CLK pin?

Thank you very much :)

# Answer
> 5 votes
"CLK" stands for "CLocK". "S" stands for "Serial". So "SCLK" is "Serial CLocK". You also get "SCL" (often used for I2C) and "SCK" meaning the same thing.

An SD card has multiple modes of communication: serial, or 4-bit parallel (called SDIO). A clock is a clock whether it is used for serial or parallel communication. What you call it doesn't really matter, at best it just gives a hint about what kind of communication it is used for.

It's like some people use MOSI/MISO and some use SDO/SDI, or DO/DI. There's no standard. It's just data moving one way, data moving the other, and some form of clock to keep it all together.

# Answer
> 1 votes
Well they both are clock pin, but on an arduino uno the sclk pin may refer to the spi clock pin while the clk may refer to the i2c clock pin. I2C and SPI are two communication protocols. So if you know which one you're using take action with the corresponding pins.

I Think the best would be for you to share the tutorial you are following and the place in the code you dont underst

---
Tags: adafruit, teensy, pcb-design
---


title: How to use Serial2 on Raspberry Pi Pico

text:
# Question
Title: How to use Serial2 on Raspberry Pi Pico
I want to use Serial1 and Serial2 in a sketch in Arduino IDE for a Raspberry pi pico. My goal is to communicate from the pico with two other Arduino boards.

I use Arduino 1.8.9 and the official board for Raspberry Pi Pico: "Arduino Mbed OS RP2040 Boards by Arduino version 2.1.0"

Serial2 does not seem to be configured. When I compile

```
    #include <Arduino.h>
    void setup() {
        Serial2.begin(9600);
    }

    void loop(){}

```

I get: `"undefined reference to '_UART2_'".`

I found some documents (not rpi pico specific) that state you can define a second UART like this:

```
    #include "wiring_private.h"
    Uart mySerial (&sercom3, 1, 0, SERCOM_RX_PAD_1, UART_TX_PAD_0); // Create the new UART instance assigning it to pin 1 and 0

```

But that throws `" 'Uart' does not name a type; did you mean 'sqrt'?"`

Does anyone have an example of how to use Serial1 and Serial2 (2 serial communications to two other boards) on a Raspberry Pi Pico using Arduino-IDE?

Thanks a lot!

# Answer
I found the solution after reading https://github.com/arduino/ArduinoCore-mbed/issues/210#issuecomment-849502556.

Although the proposed code did not compile as it was shown there.

```
UART Serial2(8, 9);  // did not compile

```

What worked for me was:

```
UART Serial2(8, 9, 0, 0);

```

Full code:

```
#include <Arduino.h>

UART Serial2(8, 9, 0, 0);

void setup() {
  Serial2.begin(9600);
  // ...
  Serial2.write("hello world");
}

void loop {}

```
> 2 votes

# Answer
I'm not using the "Mbed" board, I'm using the "Raspberry Pi Boards(1.9.7)" --\> "Raspberry Pi Pico" board, and found that:

Serial1 uses hardware UART0

Serial2 uses hardware UART1

Serial1.begin(SOME\_BAUDRATE); // defaults to TX on GP0 and RX on GP1

Serial2.begin(SOME\_BAUDRATE); // defaults to TX on GP8 and RX on GP9

To change the default UART pins to other option, as long as it comply with the Raspberry Pi Pico pinout (look for UARTx\_TX / UARTx\_RX next to the pins), see the following example:

```
#include <hardware/pio.h>

gpio_set_function(12, GPIO_FUNC_UART); // change the TX default for Serial1 (UART0)
                                       // to be GP12

gpio_set_function(13, GPIO_FUNC_UART); // change the RX default for Serial1 (UART0)
                                       // to be GP13

```
> 1 votes

---
Tags: serial, raspberrypi-pico
---


title: Emulate Joystick

text:
# Question
Title: Emulate Joystick
I am trying to automate a existing system that uses Joystick as its input to move the Motors in x&y directions. I actually want to emulate arduino to exactly do what the joystick is doing by just emulating the joystick through arduino. Joystick has 4 wires 1- 5Volt 2- Ground 3- x-axis 4- Y-axis

Actually i want arduino to map in such a way that it's only two digital pins are used to connect with joystick cable 3 & 4 and process inputs exactly in such a way that system thinks that as it is the input from the joystick. I just don't want to mess with the electronics of the existing system. That's why just trying to hack into joystick.

UPDATE: i have checked the Joystick for simple voltage and Resistance Measurements. It seems like it is 1KOhm Pot.

# Answer
> 2 votes
Maybe "DS3502 I2C Digital 10K Potentiometer" will work. I am interested to hear if it really works as a joystick pot replacement because I have never used it.

# Answer
> 2 votes
Based on your wiring information, this looks like an analog joystick. You specified using two digital output pins to emulate the joystick to the target device.

The way to do this is to use PWM and a simple resistor-capacitor filter to convert the digital output to an analog 0-5v signal.

Here is some information about PWM:

> Arduino and PWM The Arduino IDE has a built in function “analogWrite()” which can be used to generate a PWM signal. The frequency of this generated signal for most pins will be about 490Hz and we can give the value from 0-255 using this function.
>
> analogWrite(0) means a signal of 0% duty cycle.
>
> analogWrite(127) means a signal of 50% duty cycle.
>
> analogWrite(255) means a signal of 100% duty cycle.
>
> On Arduino Uno, the PWM pins are 3, 5, 6, 9, 10 and 11. The frequency of PWM signal on pins 5 and 6 will be about 980Hz and on other pins will be 490Hz. The PWM pins are labeled with ~ sign.

Here is a tutorial on converting the PWM signal to an analog voltage level:

> The RC filter is a simple circuit element used to convert a pulsed signal to a flatter signal. The key concept is that the capacitor shunts the varying voltage to ground, producing a DC voltage.

---
Tags: servo, potentiometer, joystick
---


# Question
Title: Circular buffer implementation and Serial.print()
I am trying to implement the simplest circular buffer in Arduino. I think I am able to implement it because my push and pop functions do not return any errors. However, when I want to print the data in the buffer using `Serial.print()`, the code goes into an infinite loop. Here is my code:

```
#include <stdio.h>

typedef struct circular_buffer
{
    int buffer_arr[5];
    int head;
    int tail;
} circular_buffer;

void setup()
{
  Serial.begin(9600);
  Serial.println("Circular buffer started");
}

void loop()
{
  circular_buffer *cb;
  cb_init(cb);
  if(Serial.available()){
    char instruction = Serial.read();
    Serial.println(instruction);
    if(instruction == 'a'){
      if(Serial.available()){
        char instruction = Serial.read();
        Serial.println("here");
      }
      char key = Serial.read();
      cb_push_front(cb, key);
      Serial.print(String(key));
      Serial.println("    is entered");
    }
    else if(instruction == 'd'){
      cb_pop_back(cb);
      Serial.println("last item removed");
    }
//    for(int i=0; i<5; i++){
//      Serial.print(cb->buffer_arr[i]);
//
//    }
    Serial.println("-");
  }
}

void cb_init(circular_buffer *cb)
{
    // cb->buffer_arr = {0,0,0,0,0};
    for(int i=0; i<5; i++){
      cb->buffer_arr[i] = 'c';
//      Serial.print(sizeof(*cb));
    }
//      Serial.print(cb->buffer_arr[0]);
    cb->head = 0;
    cb->tail = 0;
}

void cb_push_front(circular_buffer *cb, char item)
{
    cb->head++;
    cb->buffer_arr[cb->head] = item;
}

void cb_pop_back(circular_buffer *cb)
{
    cb->tail++;
    cb->buffer_arr[cb->tail] = 0;
}

```

The `for` loop which I commented out is the problematic one - or at least what I believe. My problem can be very much related to my poor C-language skills. I thought that `cb->buffer_arr = {0,0,0,0,0};` code piece in the initialization function should have worked (thinking very much Python-ic way) but it didn't.

So, if anyone can see the issue and help me fixing it, I'd be very much appreciated. Thanks in advance.

# EDIT

After Majenko's answer I have updated the code like that. I still have issues how to print though. I am not sure whether I am printing the address of the circular buffer as ASCII in this way. I tried to pass the `cb` to a print function in two ways. None seems to work, though...

```
#include <stdio.h>

typedef struct circular_buffer
{
    int buffer_arr[5];
    int head;
    int tail;
} circular_buffer;

circular_buffer cb;

void setup()
{
  Serial.begin(9600);
  cb_init(cb);
  Serial.println("Circular buffer started");
}

void loop()
{
  if(Serial.available()){
    char instruction = Serial.read();
    Serial.println(instruction);
    if(instruction == 'a'){
      Serial.print("a pressed");
      cb_push_front(cb, instruction);
    }
    else if(instruction == 'd'){
      Serial.print("d pressed");
    }
    else{
      Serial.print("UNKNOWN");
    }
    Serial.println("-");
  }
}

void cb_init(circular_buffer &cb)
{
    for(int i=0; i<5; i++){
      cb.buffer_arr[i] = 'c';
    }
    Serial.println("here init");
    cb_print1(&cb);
    cb_print2(cb);
    cb.head = 0;
    cb.tail = 0;
}

void cb_push_front(circular_buffer &cb, char item)
{
    cb.head++;
    cb.buffer_arr[cb.head] = item;

    cb_print1(&cb);
    cb_print2(cb);
}

void cb_pop_back(circular_buffer &cb)
{
    cb.tail++;
    cb.buffer_arr[cb.tail] = 0;
}

void cb_print1(circular_buffer *cb)
{
  Serial.println("here print1");
  for(int i=0; i<5; i++){
    Serial.print(cb->buffer_arr[i]);

  }
}

void cb_print2(circular_buffer cb)
{
  Serial.println("here print2");
  for(int i=0; i<5; i++){
    Serial.print(cb.buffer_arr[i]);

  }
}

```

And the returned output is like that:

```
here init
here print1
9999999999here print2
9999999999Circular buffer started
a
a pressedhere print1
9997999999here print2
9997999999-
a
a pressedhere print1
9997979999here print2
9997979999-

```

# Answer
You create a variable that can *point* to a circular buffer (`*cb`) but at no point do you actually point it at a circular buffer.

Either you need to create a `new` circular buffer or don't create a pointer, but create a statically allocated circular buffer. The latter is the preferred method.

Simply remove the `*` from your `cb` declaration, which makes a standard struct variable, then take the address of that (with `&`) to pass as a pointer to your other functions. Or change the other functions to pass by reference.

For example:

```
circular_buffer *cb;

```

becomes

```
circular_buffer cb;

```

And then

```
cb_init(cb);

```

becomes:

```
cb_init(&cb);

```

As do all the other functions that you pass the `cb` variable to.

Also every time you pass through `loop()` you will be wiping and making a new circular\_buffer variable. Instead `cb` should really be global and the `cb_init()` call should be in `setup()`.
> 4 votes

# Answer
There are multiple issues with this code. Majenko already showed the main one, I will add a few more here.

First, about the circular buffer implementation. As has been stated in a comment, for the buffer to be considered circular, the indices should wrap around. I would add a small nitpick: it is customary, in C++, to start filling an array from index 0. Thus you should **post**-increment (rather than pre-increment) the indices:

```
void cb_push_front(circular_buffer *cb, char item)
{
    cb->buffer_arr[cb->head] = item;
    cb->head++;                            // post-increment
    if(cb->head == circular_buffer_size)   // with wrap-around
      cb->head = 0;
}

```

There is still an issue though: the buffer can overflow. You should only add an item if there is room to do so. And you should only remove an item if the buffer is not empty. You know that the buffer is empty when tail=head, but how do you know when it is full? If you completely fill the buffer and the indices wrap around as they should, you end up with head=tail, and you cannot tell the difference between a full buffer and an empty buffer. There are two ways around this:

* You can keep an extra variable in the `struct` that helps you tell the difference. This could be a count of the elements in the buffer, or a boolean that tells you when the buffer is empty, or a boolean that tells you when it is full.
* You can refrain from completely filling the array and consider that the buffer is full when it has been filled to its full capacity, where the “capacity” is defined as the size of the array minus one. Then the buffer is full when
(head + 1) = tail \[modulo array length\].

Here is an implementation of the second strategy:

```
void cb_push_front(circular_buffer *cb, char item)
{
    int next_head = cb->head + 1;          // increment
    if(next_head >= circular_buffer_size)  // with wrap around
      next_head = 0;
    if(next_head == cb->tail){
      Serial.println("buffer overflow");
      return;
    }
    cb->buffer_arr[cb->head] = item;
    cb->head = next_head;
}

int cb_pop_back(circular_buffer *cb)
{
    if(cb->tail == cb->head) {
      Serial.println("buffer underflow");
      return -1;
    }
    char item = cb->buffer_arr[cb->tail];
    cb->tail++;
    if(cb->tail >= circular_buffer_size)
      cb->tail = 0;
    return item;
}

```

Note that there is no point in `cb_pop_back()` overwriting the buffer. On the other hand, it is often very useful for this function to return the item that has just been popped.

Now, a few random comments:

* Rather than hard-coding the array size (`5`) in multiple places, define a constant at the beginning of the program (`circular_buffer_size` in my examples) and use it everywhere. This makes it clearer what the number means, and makes it easier to change the size if you ever need to.
* Since you are storing characters, use an array of `char`.
* Your code for handling `Serial` does not cope well with the situation where there is a small delay between the `'a'` instruction and the character to be added. If you can afford blocking code, the simplest solution is to block until the user provides the character:

```
if(instruction == 'a'){
  while(!Serial.available())
    ;  // wait for the key to be entered
  char key = Serial.read();
  cb_push_front(&cb, key);
  Serial.print(key);
  Serial.println(" is entered");
}

```
> 1 votes

---
Tags: serial, loop
---


# Question
Title: Inconsistent behavior of include statements?
This question deals with fonts. Well, actually, singular include files with constants that form bitmap fonts or images for LCD or OLED displays, such as this one: https://github.com/ThingPulse/esp8266-oled-ssd1306/blob/master/src/OLEDDisplayFonts.h. A strangely hard to research (but common) problem occurs when anyone attempts to include one of these font files in multiple files in an application.

Even with an include-guard present, reuse of this .h file will cause strange linker errors about redefinition of a variable. It turns out that any variable actually declared in a .h file will cause it to be created multiple times in the program, and then when the different files depending on the font.h file are linked, the variables collide. An example of it would be here: https://stackoverflow.com/a/4936294/11381501, or in my very own question some time ago here: How to create large progmem arrays and not annoy the linker

The issue is, I've seen some libraries that avoid this error (such as https://github.com/ThingPulse/esp8266-oled-ssd1306/blob/master/src/OLEDDisplay.h#L64) despite having variables declared in .h files. I've even included this OLEDDisplay.h file (indirectly via the main library file for my screen) in multiple of my own .h files in one project and it hasn't caused linker errors. What causes *this* globally-scoped variable in a .h file to not produce linker errors when my own code usually does?

# Answer
> 1 votes
The esp8266-oled-ssd1306 has it wrong, but with examples of the library it is saved by optimization. Other files where the font gets included don't use it so it is optimized away. Otherwise it would be duplicated.

OLEDDisplayFonts.h is included in OLEDDisplay.h which is included in OLEDDisplay.cpp and in SSD1306Wire.h which is included in the sketch. This creates two instances of the variables defined in OLEDDisplayFonts.h. But the instance in OLEDDisplay.cpp is optimized away, because that file doesn't use fonts. So for the linker there is only one instance of the variable.

relevant part from .map file for the SSD1306UiDemo.ino example

```
 .irom.text.OLEDDisplayFonts.h.857.11
                0x000000004023b7fd     0x25ab ./SSD1306UiDemo.ino.cpp.o
 .irom.text.OLEDDisplayFonts.h.434.10
                0x000000004023dda8     0x13b9 ./SSD1306UiDemo.ino.cpp.o
 .irom.text.OLEDDisplayFonts.h.10.9
                0x000000004023f161      0xaab ./SSD1306UiDemo.ino.cpp.o
 .irom.text.OLEDDisplayFonts.h.10.9
                0x000000004023fc0c      0xaab ./libraries/esp8266-oled-ssd1306/src/OLEDDisplay.cpp.o
 .irom.text.OLEDDisplayFonts.h.10.9
                0x00000000402406c7      0xaab ./libraries/esp8266-oled-ssd1306/src/OLEDDisplayUi.cpp.o

```

The right way is to put variables in a cpp or c file and if they should be visible from other file, then use `extern` keyword in that file (usually over an included .h file) to declare that the variable exists.

---
Tags: variables, linker
---


# Question
Title: Odd behavior of defines - is this Arduino specific and how does it work?
A question I have long had regards an interaction between `#define` statements and a library header include. I've seen several Arduino libraries use this rather convenient configuration system where you can write a `#define` statement, then include the `#include header` instruction and the library will change its behavior.

Does anyone know why this does or does not work? My understanding of how `#include` works is that it copies the file into its place in the new file and that C/C++ code with headers should thus form something of a tree shape. It would thus make sense that any lines of code present before I `#include` a library would have to be seen by the library when the preprocessor does its thing and copies the `#include`-d file.

But I've previously been told it doesn't actually work this way (despite actual examples, but by that point I had misplaced the libraries that did it). However, I've finally tracked down an example of someone doing this, and I'd like to know how this behavior works. Is it a feature of Arduino in specific or something?

Here's a library that does it: https://github.com/madpilot/mDNSResolver

# Answer
> 1 votes
The mDNSResolver library README has

> You can change a few settings by defining some constants before including the mDNSResolver header file *\[in the sketch\]*

*I added "in the sketch", because it is where **you** include mDNSResolver header file.*

This will not work because mDNSResolver.cpp, which uses the values of the defines, is compiled without any knowledge of the ino file content.

*A deeper explanation would be too long and out of scope for Arduino SE.*

# Answer
> -1 votes
In the first few lines of the library .h file you linked, it tests a symbol it has not defined, ESP32 (`#if defined(ESP32)`) and alters the compilation according to the definedness of that symbol.

Since the compiler input will be one long file with all the #include files already "spliced" inline, you (or a previously included .h file) have the option to pre-define that variable to alter the compilation as this library writer provided.

Update:

@RDragonflydr: I believe you are correct, that the ESP32 symbol is defined by the compiler (or more likely, the makefile) for the target processor. And to clarify my statement that "the compiler input will be one long file", I'm referring to the compilation of each module of your project, not to the entire project at once. Each library will each be compiled, separately, and each of your modules will be compiled separately, and in *each of those cases*, the compiler input will consist of the module code with all of the #included files "spliced" into place, the #defined symbols and macros substituted, and comments stripped out.

The library code (the .cpp) is not #included so it does not become part of the translation unit. Only the library header file is included. That is common - and necessary - to all environments, and one of the functions of the C or C++ preprocessor. This happens for each module - separately - in a multi-module compilation. The library files will have been

---
Tags: library, c-preprocessor, header
---


# Question
Title: ESP32 programing error (A fatal error occurred: Timed out waiting for packet content )
I am trying to find the proper model of my esp32 MCU and find out the reason for below errors:

```
esptool.py v3.1-dev
Serial port /dev/ttyUSB0
Connecting........__
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, Coding Scheme None
Crystal is 40MHz
MAC: 30:ae:a4:26:6f:44
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Configuring flash size...

A fatal error occurred: Timed out waiting for packet content
A fatal error occurred: Timed out waiting for packet content

```

My ESP32 type and the board setting are shown below (link) :

Thanks.

# Answer
> 1 votes
In case where the upload procedure with esptool.py starts successfully but fails as soon as the baud rate is set to the high baud rate selected in Tools menu, select a lower baud rate in Tools menu.

# Answer
> 1 votes
This can also be caused by how certain pins are wired, if pin 12 is high at boot, programming will also fail with this message.

---
Tags: esp32
---


title: Arduino shield stacking issue

text:
# Question
Title: Arduino shield stacking issue
I'm having issues stacking shields on my Arduino Mega 2560. I would like to add to the mega a ethernet shield (https://store.arduino.cc/products/arduino-ethernet-shield-2) and on top of that the dragino lora shield v1.4 (https://wiki.dragino.com/index.php?title=Lora\_Shield). When I connect only the dragino lora shield to the mega I do get the lora to initialize but the moment I add the ethernet shield to the mega and the dragino lora shield to the ethernet shield the lora initialization fails. Is this stacking possible and if so any advice on how to get all to work together?

Thanks in advance.

# Answer
> 3 votes
Both those boards use pin 10 for chip select by default. You will need to change that.

Probably the simplest one to change is the LoRa board. If you look at the WiKi page you link to it lists a set of resistors and jumpers which can be altered to modify the connectivity of the board. By default R9 is populated which connects to pin 10. If you move that to the R10 position next to it you will instead be connecting to D5 for the chip select pin. Moving surface mount resistors can be tricky though so you may want to practice working on a scrap board first.

And of course when you have changed the chip select pin you will also have to change your code to use that new chip select pin instead.

---
Tags: shields, ethernet, lora
---


# Question
Title: Weird SoftwareSerial behaviour with Arduino nano when multiple SoftwareSerial objects are configured
So I have been stuck with this for quite some days now and am really unable to put my finger on what's wrong here. Any help would be great. Try not to list down the disadvantages of SoftwareSerial, I know about them. Is this issue happening cause of it?

**What I am trying to achieve?** Two Arduino (Nanos) are connected to each other and are trying to communicate with each other via SoftwareSerial UART (multiple Arduinos are connected to multiple Arduinos but for the sake of keeping the code minimalistic, assume one-to-one communication).

**Code for Arduino 1**:

```
#include <SoftwareSerial.h>

String val = "4";

SoftwareSerial a(6, 7); // rx, tx
SoftwareSerial b(8, 9); // rx, tx
SoftwareSerial c(10, 11); // rx, tx
SoftwareSerial d(12, 13); // rx, tx

String a_s, b_s, c_s, d_s;

void setup() {
  a.begin(9600);
  b.begin(9600);
  c.begin(9600);
  d.begin(9600);
  Serial.begin(9600);
  a_s = "";
  b_s = "";
  c_s = "";
  d_s = "";
}

void loop() {
  delay(100);

  d.print(val);

  if(d_s == "") {
    if(d_s == "" && d.available()) {
      char rd = d.read();
      Serial.print("ddddddddddd ");
      Serial.println(rd);
      d_s = rd;
    }
  }
}

```

**Code for Arduino 2:**

```
#include <SoftwareSerial.h>

String val = "5";

SoftwareSerial a(6, 7); // rx, tx
SoftwareSerial b(8, 9); // rx, tx
SoftwareSerial c(10, 11); // rx, tx
SoftwareSerial d(12, 13); // rx, tx

String a_s, b_s, c_s, d_s;

void setup() {
  a.begin(9600);
  b.begin(9600);
  c.begin(9600);
  d.begin(9600);
  Serial.begin(9600);
  a_s = "";
  b_s = "";
  c_s = "";
  d_s = "";
}

void loop() {
  delay(100);

  b.print(val);

  if(b_s == "") {
    if(b.available()) {
      char rd = b.read();
      Serial.print("bbbbbbbbbbbb ");
      Serial.println(rd);
      b_s = rd;
    }
  }
}

```

**Actual and Expected output for Arduino 1:** `ddddddddddd 5`

**Actual output for Arduino 2: (no output!!)**

**Expected output for Arduino 2:** `bbbbbbbbbbbb 4`

While debugging, I realized if I comment out c.begin(9600) and d.begin(9600) from the code for Arduino 2, the actual output is the same as the expected output.

```
//   c.begin(9600);
//   d.begin(9600);

```

Ohh and yes, the connections are as follows:

| **ard1** | **ard2** |
| --- | --- |
| GND | GND |
| D12 | D9 |
| D13 | D8 |

Both the Arduinos are connected (and hence powered) via individual cables to the same laptop. Is there something I am missing out on? I know SotfwareSerial has its downsides but is this something SoftwareSerial is causing? If yes, why is the actual output of Arduino 1 is same as the expected output? Shouldn't it have the same effect on both the Arduinos?

TIA!

# Answer
> 1 votes
Is one of the downsides of SoftwareSerial that you know about the fact that *only one SoftwareSerial can be set to receive data at a time*?

You have to select one *and only one* instance to receive data using the `.listen()` method.

If you want to be able to receive from multiple devices at once you will have to use either hardware UARTs or a single SoftwareSerial and a "bus" infrastructure (RS485 or similar).

---
Tags: arduino-nano, softwareserial, uart
---


title: Integer division

text:
# Question
Title: Integer division
I want to divide two integers and get how often one of them fits into the other completely, so there will always be an integer as an output. In python you do this kind of operation with `//`.

# Answer
> 4 votes
In C++, the integer division operator is `/`, and the floating point division is also `/`. The compiler chooses according to the types of the operands:

* if either or both operands are floating points numbers, you get a floating point division
* if both operands are of some integer type, you get an integer division.

Thus:

* `5.0/2.0`, `5.0/2`, `5/2.0` all have the same value, namely `2.5`
* `5/2` is `2`.

---
Tags: arduino-uno, c++
---


# Question
Title: On an esp8266 what is faster, 64-bit math or float math?
I need to do some math with vars from an attitude sensor (acclerometers and gyroscopes) on an esp8266. int32\_t math with those vars does not have enough range and the float math operations are abyssmally slow.

I'm hoping to use a fixed-point format with 24 bits on the left of the decimal point and 8 bits on the right side. So a number would be stored in a int32\_t as 0xffffff.ff. A typical operation would be a multiply like ((i64) x * y) \>\> 24).

So 64-bit math is needed. Would this be significantly slower than a float multiply?

# Answer
> 11 votes
Calculating the relative performance of 64 bit integer versus floating point multiplication is a little more difficult than it would appear.

It's easy to time a loop that does thousands of calculations, but unless you do something with the result the compiler will happily optimize them away. It's tempting to multiply constants, but the compiler will also happily optimize that away.

To keep the code as simple as possible and not introduce anything into the loop that would take more time, you need to disable or bypass the compiler's optimizations. An easy way to do that is to use the `volatile` modifier on the variables. `volatile` tells the compiler that it can't assume the variable hasn't changed, so the compiler will disable optimizations like caching the variable in a register or noticing that the same calculation is being done over and over again and just doing it once.

Using this technique, with a couple of tests to verify that the compiler would perform optimizations that interfere with timing, I get these results:

```
non-volatile uint32_t microseconds 1
constants uint32_t microseconds 5
uint16_t microseconds 162513
uint32_t microseconds 137513
uint64_t microseconds 1287533
int16_t microseconds 337513
int32_t microseconds 300023
int64_t microseconds 1287513
float  microseconds 912529

```

64 bit unsigned or signed multiplication takes about 1.4x the time as floating point multiplication.

The first test verified that non-volatile variables are optimized out. The second test verified that multiplying constants is also optimized out.

Turning off Wifi also helps keep test results consistent.

```
#include <Arduino.h>

#ifdef ESP32
#include <WiFi.h>
#else
#include <ESP8266WiFi.h>
#endif

#define TIMES_TO_LOOP 1000000

volatile uint16_t ux16, uy16, uresult16;
volatile uint32_t ux32, uy32, uresult32;
volatile uint64_t ux64, uy64, uresult64;

volatile int16_t x16, y16, result16;
volatile int32_t x32, y32, result32;
volatile int64_t x64, y64, result64;

volatile float xf, yf, resultf;

uint32_t x32n, y32n, result32n;

uint16_t seed16() {
  return random(0, 0xffff);
}

uint32_t seed32() {
  return random(0, 0xffffffff);
}

uint64_t seed64() {
  return seed32();
}

float seedfloat() {
  float x, y;

  x = seed32();
  y = seed32();

  return x / y;
}

void setup() {
  uint32_t i;
  uint64_t micros_start, micros_end;

  Serial.begin(115200);
  Serial.println("hello world");

  WiFi.mode( WIFI_OFF );

  delay(1000);

  x32n = seed32();
  y32n = seed32();

  micros_start = micros();
  for(i = 0; i < TIMES_TO_LOOP; i++)
    result32n = x32n * y32n;
  micros_end = micros();

  Serial.print("non-volatile uint32_t microseconds ");
  Serial.println(micros_end - micros_start);

  micros_start = micros();
  for(i = 0; i < TIMES_TO_LOOP; i++)
    result32n = 540000 * 15;
  micros_end = micros();

  Serial.print("constants uint32_t microseconds ");
  Serial.println(micros_end - micros_start);

  ux16 = seed16();
  uy16 = seed16();
  x16 = ux16;
  y16 = uy16;

  micros_start = micros();
  for(i = 0; i < TIMES_TO_LOOP; i++)
    uresult16 = ux16 * uy16;
  micros_end = micros();

  Serial.print("uint16_t microseconds ");
  Serial.println(micros_end - micros_start);

  ux32 = seed32();
  uy32 = seed32();
  x32 = ux32;
  y32 = uy32;

  micros_start = micros();
  for(i = 0; i < TIMES_TO_LOOP; i++)
    uresult32 = ux32 * uy32;
  micros_end = micros();

  Serial.print("uint32_t microseconds ");
  Serial.println(micros_end - micros_start);

  ux64 = seed64();
  uy64 = seed64();
  x64 = ux64;
  y64 = uy64;

  micros_start = micros();
  for(i = 0; i < TIMES_TO_LOOP; i++)
    uresult64 = ux64 * uy64;
  micros_end = micros();

  Serial.print("uint64_t microseconds ");
  Serial.println(micros_end - micros_start);

  micros_start = micros();
  for(i = 0; i < TIMES_TO_LOOP; i++)
    result16 = x16 * y16;
  micros_end = micros();

  Serial.print("int16_t microseconds ");
  Serial.println(micros_end - micros_start);

  micros_start = micros();
  for(i = 0; i < TIMES_TO_LOOP; i++)
    result32 = x32 * y32;
  micros_end = micros();

  Serial.print("int32_t microseconds ");
  Serial.println(micros_end - micros_start);

  micros_start = micros();
  for(i = 0; i < TIMES_TO_LOOP; i++)
    result64 = x64 * y64;
  micros_end = micros();

  Serial.print("int64_t microseconds ");
  Serial.println(micros_end - micros_start);

  xf = seedfloat();
  yf = seedfloat();

  micros_start = micros();
  for(i = 0; i < TIMES_TO_LOOP; i++)
    resultf = xf * yf;
  micros_end = micros();

  Serial.print("float  microseconds ");
  Serial.println(micros_end - micros_start);
}

void loop() {
}

```

# Answer
> 6 votes
OK, I took the trouble to code fixed-point routines and test them. Here is the code ...

```

// fixed-point routines for 20 bit integer / 12-bit fraction
// format 0xfffff.fff, max value is +-524288.9998

#define FP_I 20     // integer bits,  e.g. 0xfffff.0
#define FP_F 12     // fraction bits, e.g. 0x0.fff
#define fpMul(_x,_y) ( (i32)( ((i64) _x * _y) >> FP_F) )
#define fpDiv(_x,_y) ( (i32)(  (((i64) _x) <<   32)      \
                             / (((i64) _y) >> FP_I) ) )
#define decToFp(_dec) ((i32) (_dec * 4096))   // e.g. decToFp(-1.2)
#define fpToI32(_fp)  ((i32) (_fp  / 4096))

```

I did a 1000 loops and used the loop counter as the operation argument. So each loop was one operation plus one floating-point increment. I did this for fixed-point multiply, floating-point multiply, fixed-point divide, and floating-point divide.

The results were shocking. The fixed-point multiply took almost exactly the same time as the floating-point. The fixed-point divide took 60% longer than the floating-point. I went over my code and tried everything I could think of. I built it in debug and release mode. I tried 80MHz and 160MHz. I found floating-point speeds on the web and my floating-point tests closely matched those.

I am at a loss as to how the 64-bit math could be slower than the floating-point. Both are implemented in software. I'm sure the floating-point is highly optimized with some tricks and coded in machine language. But my fixed-point code is just shifts and 64-bit multiply/divides. I don't think there are any tricks that could be used for it.

At least this makes the decision easy. I guess I'll use floating-point as sparingly as possible and cross my fingers that I can get my app to work fast enough.

I hope this is helpful to some passing-by googlers. I couldn't find anything when I googled it.

---
Tags: esp8266, speed, mathematics
---


# Question
Title: 64 led strips and 8 shift registers (connected to 64 sensors) connected to 5V and ground of arduino
I have connected 64 led strips and 8 shift registers (connected to 64 sensors) to 5V and ground of arduino. Voltage received at led strip is 3.1V and at registers is 3.2V. I plan to add an led display as well a small buzzer to it. Can I connect it to the same 5V? What should I make sure in this in terms of voltage supply so that there is no less supply as well loss of power? What if I add a 9V battery instead?

Note: I am just a beginner in this topic.

# Answer
> 3 votes
I guess you are currently powering all this through a USB port on your PC. You are lucky if you haven't fried anything on the Arduino. When the 5V pin goes down to 3.1V this means that either you have fried the power electronics or you are currently totally stressing the power supply out. Power supplies, which are overloaded often drop the voltage significantly, or just fail outright.

All the LEDs, shift registers and sensors draw current. With this much LED strips you can easily get into the territory of many amps of current needed for driving them (depending on the number of LEDs and the current draw of each LED). A PC USB port will only provide 500mA by default. An Arduino can handle a current output from the 5V pin of about 1A, including the current, that the Arduino itself needs. So lets say 800mA. When you provide power through the barrel jack this also depends on the voltage of your power supply. The linear voltage regulator dissipates the voltage difference to 5V as heat, so supplying a higher voltage means more heat, means it can handle less current.

**What to do now:**

* You need to power your LED strips, shift registers and sensors directly from your power supply. The current for them should not go through the Arduino. If you use a power supply with a higher voltage than 5V, you need to buy a voltage regulator for 5V that is capable of providing the total needed current (+ at least 20% headroom to be safe and reduce heat). Go for a switching "buck" converter type, not for a linear converter. They are way more efficient and provide less heat. There are ready-to-use modules available on the typical sides (for example the one named like the biggest river). Often they have a small potentiometer on them, which you can spin with a small screwdriver to set the wanted output voltage (here 5V).
* Make sure, that the used power source is actually capable of providing the total needed current. For this you first need to calculate how much current the project will draw. For example: Standard addressable LED strips with WS2812 will draw 60mA per LED when on full brightness white. Multiply with the number of LEDs to get the total current for them. GO through all components of your project and write down the current draw (look at the datasheet for such info). Add it all up. Then get a power source (and if needed voltage regulator) that can handle at least 20% more than that.

> What if I add a 9V battery instead?

As written above you will need an external voltage regulator for your project. Also: If you mean the standard 9V block batteries, then just forget it. They are not meant for high power applications and can only provide very low current. You should look in the direction of LiIon or LiPo batteries. When you use them please also buy a fitting charger module (since these are rather particular about how they want to be charged).

But before choosing a battery you first need to know how much current your project draws. Then you need to set a time range how long the project should run on the battery without charging. Then you can choose a battery based on that.

---
Tags: arduino-uno, electronics
---


title: Using Analog Pins for Power

text:
# Question
Title: Using Analog Pins for Power
I'd like to power something needing 5V DC using the Arduino Uno analog pins. Specifically, I'm following the "Fun Plug-in Hack for Arduino UNO" section of this Adafruit tutorial. It states:

> "If you set analog pin A3 to an OUTPUT and HIGH and A2 to an OUTPUT and LOW you can power the RTC directly from the pins!"

It doesn't seem to work, so I try this with a multimeter hooked up to A2 & A3:

```
void setup() {
  pinMode(A2, OUTPUT);
  pinMode(A3, OUTPUT);
  analogWrite(A2, LOW);
  analogWrite(A3, HIGH);
}

void loop() {}

```

I only measure ~0.8V, trying 2 boards including a brand-new unboxed one. This is the same whether the board is powered via USB or via 9V battery in the barrel jack.

But I *can* measure ~5V from those pins if I set the "ground" pin as INPUT\_PULLUP (using the built-in pull-up resistor) instead of OUTPUT:

```
void setup() {
  pinMode(A2, INPUT_PULLUP);
  pinMode(A3, OUTPUT);
  analogWrite(A3, HIGH);
}

void loop() {}

```

I don't believe Adafruit is wrong, since conceptually it makes more sense to set both pins to OUTPUT, with one being HIGH and the other being LOW. So what am I doing wrong?

# Answer
> 2 votes
The analog pins are only analog *inputs* but *digital* outputs (and inputs too). You use `digitalWrite()` to control a pin's HIGH or LOW state.

`ànalogWrite()` is for use with the PWM pins (I hate the name the chose for that function it's so wrong on so many levels).

Also this powering trick only works for things that need very small currents (\< about 20mA) since that is the maximum an IO pin can deliver.

---

Incidentally, you *can* use `analogWrite()` on a non-PWM pin, but it's either fully on (analog value \>= 127) or fully off (analog value \< 127). The macros HIGH and LOW are 1 and 0 respectively, so that equates to "fully off" and "fully off" on a plain non-PWM pin.

---
Tags: arduino-uno, power, analog
---


# Question
Title: How do I solve the sensor’s abysmal abnormal output?
## Problem description

When XIAO microcontroller is connected to the sensor signal output pin, keep printing unexpected values.

Sketch   I haven't sensor circuit diagram, you can machine translate and read the pagehttps://www.yuque.com/cs/products/rwe87o

## Detailed description:

The sensor has been outputting a raw value of about 4000. That is to say, the transformation of the function outputs is around 1600000~400000. Not the 0 to 30 values I expected to output.(I tried to use other development board ESP32 to read the sensor value using ADC and its own power supply pin, and still output the original value of 4000(output 1600000~400000), as shown in the figure below.)

### Abnormal output

### Normal output:

This is the output I expect, but I can't reproduce the normal output (last month was very occasionally normal output, I don't know why it can't normal output now and the environment has not changed...Analog circuits are too much like witchcraft...)

### I try to do:

1. Measured with a digital multimeter to ESP32 and XIAO, the power supply pin(to measure voltage between 3.3V and GND pin) is about 3.1V~3.3V(maybe my positive and negative probes are not accurate).

2. Check XIAO's ADC using ADC pin to GND pin for any problems with Analog pins read GND and VCC respectively, with 12bit resolution and output of 100 and 4000 + respectively.XIAO is fine.
3. The Sensor connects the `TTL to USB` power supply pin, but the output is still abnormal.(I used PWM pin by esp32 or xiao MCU to output analog 3.3V, results same to abnormal output)
4. If the sensor signal output is connected to MCU, the high value of 4000 is output(The original value). Seems to be the power problem, someone tell me how to minimize the power ripple effect and make the 3.3V stable output provided by the battery? But when I unplug the sensor, the MCU outputs random values, and I don't want the sensor output to be affected by other factors.

5. DMM Checks the sensor output and GND pin separately, using only the DMM, sensor, 3.3V battery module, and breadboard： (This is one 3P Dupont line and the colors are random.I didn't have a wiring error. If the wiring is wrong neither the MCU nor the sensor will work properly, let alone output signals) The quality of my DMM is poor, I will measure it when my FLUKE17B+ arrives.

Use MCU to read the value, sure enough, the output value is not affected by the power on or off, because the output value is more than 100000(raw value is 4000,12 bit).

## other

Asked the store, customer service said this is not normal, the sensor should output the value of 0~500, if the sensor monitors the current output of 200~400, not monitoring the general output of 0~30 range.

## Equipment information

XIAO EMG sampled code: EMG.ino · GitHub

Sensor parameters: {ED0056}Sensor

Microcontrollers XIAO general description: Getting Started with Seeeduino XIAO - Seeed Wiki

Microcontrollers XIAO instruction manual: https://files.seeedstudio.com/wiki/Seeeduino-XIAO/res/ATSAMD21G18A-MU-Datasheet.pdf

How do I solve XIAO’s the sensor’s abnormal output?

# Answer
> 1 votes
First thing I notice in your photo is that you have your 3.3V and GND wires around the wrong way on the Xiao microcontroller board. This will indeed give you wrong results.

Please see the image below for the pinout.

Your Fritzing circuit diagram has the correct wiring for 3.3V and GND.

Please do a couple of tests to verify your setup once the wiring has been corrected. First, remove the sensor and use a potentiometer to provide an analog input into A0. Verify that the potentiometer gives you the correct response that you expect and cross check it with an oscilloscope or digital multi-meter (DMM).

Secondly, power up only the sensor and test the output of the sensor with an oscilloscope or DMM. This should how up as the clean analog signal that you expect.

If these two tests pass, then when you connect up the sensor to the Xiao microcontroller board it should work as expected.

# Answer
> 1 votes
Now it solved. I was so stupid, because I didn't connect the MCU to the sensor power, so the MCU couldn't measure the sensor value with the reference power.

---
Tags: analog-sampling, seeeduino-xiao
---


title: String Comparison

text:
# Question
Title: String Comparison
I am trying to compare two string but each time i am getting awkward results. When i use Equals() function i always get the wrong answer and never satisfy the IF statement. When i use compareTo() function i always get the right answer (which is btw not correct) but it always just satisfy the IF statement.

```
String input;
String Answer = "cat";

void setup()
{
  Serial.begin(19200);
}

void loop()
{
  while (Serial.available() == 0)
  {
    Serial.readStringUntil('\n');
    if (input.equals(Answer))
    {
      Serial.println(input + ", Correct");
    }
    else
    {
      Serial.println(input + ", wrong");
    }
  }
}

```

Above is the code i am using to learn the string Comparison.

# Answer
You have defined a variable named `input`, but you never assign a value to it. It should thus always have its initial value, which is an empty string.

Here:

```
Serial.readStringUntil('\n');

```

you probably meant

```
input = Serial.readStringUntil('\n');

```
> 1 votes

---
Tags: arduino-ide, string
---


# Question
Title: How to prevent replacing of the value for a key in Preferences?
```
#include <Preferences.h>

Preferences preferences;

const char* roomname_long = "String 1";
const char* roomname_short = "String2";
const char* roomname_long_test = "String 3";
String roomname;

void setup() {
  Serial.begin(115200);
  preferences.begin("app",false);

  preferences.begin("credentials", false);
  preferences.putString("roomname_long", roomname_long);
  preferences.putString("roomname_short", roomname_short);
  preferences.putString("roomname_long", roomname_long_test);

  roomname = preferences.getString("roomname_long", "");

  Serial.println(roomname_long);
  Serial.println(roomname_short);
  Serial.println(roomname);

  preferences.end();
}

void loop() {

}

```

I try to save some data on my esp32 it works actually but if I try to save another variable with the same key it writes over the String before how can I interrupt that?

on the Serial Monitor I got this:

```
rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:1324
ho 0 tail 12 room 4
load:0x40078000,len:13508
load:0x40080400,len:3604
entry 0x400805f0
String 1
String 2
String 3

```

I want that if I put another variable in "roomname\_long" key it must not be overwritten.

So it should be stay on String 1

# Answer
> 0 votes
You can use `isKey` to test if the preferences key is already set.

```
preferences.putString("roomname_long", roomname_long);
preferences.putString("roomname_short", roomname_short);
if (!preferences.isKey("roomname_long")) {
  preferences.putString("roomname_long", roomname_long_test);
}

```

---
Tags: esp32
---


# Question
Title: Playing a sound file with Arduino Uno Wifi
I have an Arduino Uno Wifi (Rev2) and I want to play a sound file. I found two libraries so far but neither of them work. The TMRpcm library (https://www.arduino.cc/reference/en/libraries/tmrpcm/) only works on the base Arduino Uno and anther library called AudioZero (https://www.arduino.cc/reference/en/libraries/audiozero/) does not seem to be compatible with Arduino Uno Wifi. Is there any other library that I could use to play a sound file. I have an SD card attached and a library that can read in a .wav file but no library to actually output the sounds on a basic speaker.

I need to use the Arduino Uno Wifi for what I am trying to achieve since communicating with the internet is crucial. Using a base Uno with a wifi attachment did not prove to be promising since the attachment acted as it's own board and need to be flashed.

# Answer
> 1 votes
No sounds library seems to exist that is compatible with the architecture of the Arduino Uno Wifi specifically. Other micro-controllers need to be looked into.

---
Tags: arduino-uno, arduino-uno-wifi
---


# Question
Title: Can't get Adafruit MusicMaker and Ethernet to work at the same time
Working on an IoT project.
Environment:

* Adafruit Feather M4 Express
* Adafruit MusicMaker FeatherWing (amplified, this is a VS1053 at heart)
* Silicognition PoE FeatherWing (electrically compatible with the Adafruit Ethernet FeatherWing)
* PlatformIO with Visual Studio Code on a Mac (all libraries are up-to-date)

The Problem:
I can get either the Ethernet to work or the MusicMaker to work, but not both at the same time.
There are no errors during initialization, they just don't work.

Specifically, the MusicMaker's `sineTest()` method doesn't generate any sound if Ethernet has been initialized. Likewise, the Ethernet client's `connect()` method is unable to connect if the MusicMaker is initialized. Both methods work fine if the other device hasn't been initialized.

Feather Pins:

```
Purpose                    Pin
-------------------------- ---
VS1053 Chip select           6
VS1053 Data/command select  10
VS1053 Data request          9
SD Card Chip select          5
Ethernet Chip select        14

```

Note that I'm not currently using the SD card and haven't called its `begin()` method.
Note also that the Ethernet chip select is on a non-standard pin because the default conflicts with the VS1053 data/command select.

Also, the PoE FeatherWing has an EEPROM on it with a unique MAC address and reading this works fine.

One other note: I thought that I had this combination working in an earlier prototype a few months ago, but that may have been with the Feather 32U4.

TIA.

The prototype code:

```
#include <Arduino.h>
#include <Adafruit_VS1053.h>
#include <Ethernet.h>
#include <IPAddress.h>
#include <SPI.h>
#include <Wire.h>

//
// Pins for the Ethernet and MusicMaker FeatherWings.
// NOTE: These definitions assume a Feather M4 Express with a PoE FeatherWing
//       modified to have its chip select on pin 14 because the default conflicts
//       with the MusicMaker's DCS pin.
//
#define VS1053_CS       6     // VS1053 chip select pin (output)
#define VS1053_DCS     10     // VS1053 Data/command select pin (output)
#define SD_CARD_CS      5     // Card chip select pin
#define VS1053_DREQ     9     // VS1053 Data request, ideally an Interrupt pin
#define ETHERNET_CS    14     // Ethernet chip select pin. Note that this is custom because the default conflicts with VS1053_DCS.

//
// PoE FeatherWing.
//
#define MAC_EEPROM_I2C_ADDRESS      0x50  // I2C address of the 24AA02E48 EEPROM chip that contains our MAC address (on the PoE FeatherWing).
#define MAC_EEPROM_REGISTER         0xFA  // Register within the 24AA02E48 that contains the first byte of our MAC address.

#define HANG      while( true ){ delay( 10 ); }
IPAddress         host = { 172, 24, 110, 1 };
uint16_t          port = 8000;
const char*       path = "/sounds/DingDong.mp3";
Adafruit_VS1053   player{ Adafruit_VS1053( -1, VS1053_CS, VS1053_DCS, VS1053_DREQ ) };
EthernetClient    client;
uint8_t           mp3Buf[VS1053_DATABUFFERLEN];
uint32_t          contentLength;
byte              macAddress[6]{};

uint32_t          beginRequest();
void              readMacAddress( byte* );

void setup()
{
  Serial.begin( 115200 );
  while( !Serial ){ delay( 10 ); }
  Serial.println( "MusicMaker/Ethernet Prototype" );

  Wire.begin();

  Ethernet.init( ETHERNET_CS );
  readMacAddress( macAddress );
  if( !Ethernet.begin( macAddress ))
  {
    Serial.printf( "Unable to initialize the network; hardware status is %d\n",
                   Ethernet.hardwareStatus());
    HANG;
  }

  Serial.print( "Mac address: " );
  for( int ii = 0; ii < 6; ii++ )
  {
    if( macAddress[ii] < 16 ) { Serial.print( '0' ); }
    Serial.print( macAddress[ii], HEX );
    if( ii < 5 ) { Serial.print( ':' ); }
  }
  Serial.println();
  Serial.print( "DNS IP:      " ); Serial.println( Ethernet.dnsServerIP());
  Serial.print( "Local IP:    " ); Serial.println( Ethernet.localIP());
  Serial.print( "Gateway IP:  " ); Serial.println( Ethernet.gatewayIP());
  Serial.print( "Subnet mask: " ); Serial.println( Ethernet.subnetMask());

  if( !player.begin())
  {
    Serial.println( "Unable to initialize the MusicMaker" );
    HANG;
  }
  Serial.println( "Initialized the MusicMaker" );
  player.setVolume( 40, 40 );
  player.sineTest( 0x44, 1000 ); // 1KHz tone for one second.

  contentLength = beginRequest();
  Serial.printf( "HTTP content length: %d\n", contentLength );
}

void loop()
{
  if( player.readyForData())
  {
    if( client.available() > 0 )
    {
      uint8_t bytesRead = client.read( mp3Buf, VS1053_DATABUFFERLEN );
      if( bytesRead > 0 )
      {
        player.playData( mp3Buf, bytesRead );
        contentLength -= bytesRead;
        if( contentLength <= 0 )
        {
          Serial.println( "That should be all of our sound" );
        }
      }
    }
  }
}

uint32_t beginRequest()
{
  if( !client.connect( host, port ))
  {
    Serial.print( "Unable to connect to " );
    Serial.print( host ); Serial.print( ':' ); Serial.println( port );
    HANG;
  }

  Serial.print( "GET " ); Serial.print( path ); Serial.print( " HTTP/1.1\r\n" );
  Serial.print( "Host: " ); Serial.print( host ); Serial.print( "\r\n" );
  Serial.print( "Connection: close\r\n\r\n" );

  client.print( "GET " ); client.print( path ); client.print( " HTTP/1.1\r\n" );
  client.print( "Host: " ); client.print( host ); client.print( "\r\n" );
  client.print( "Connection: close\r\n\r\n" );

  char http[] = "HTTP/";
  client.find( http );                  // Skip over the HTTP/ part of the header.
  client.parseFloat( SKIP_WHITESPACE ); // Skip over the HTTP version number.

  int httpStatus = client.parseInt( SKIP_WHITESPACE );
  if( httpStatus < 200 || httpStatus > 299 )
  {
    Serial.printf( "GET request failed; HTTP status is %d\n", httpStatus );
    client.stop();
    HANG;
  }

  char lengthHeader[] = "Content-Length:";
  client.find( lengthHeader );
  int contentLength = client.parseInt();

    char endOfHeaders[] = "\r\n\r\n";
  if( !client.find( endOfHeaders ))
  {
    Serial.println( "Invalid HTTP response (missing trailing line endings)" );
    client.stop();
    HANG;
  }
  return contentLength;
}

void readMacAddress( byte* addr )
{
  Wire.beginTransmission( MAC_EEPROM_I2C_ADDRESS );
  Wire.write( MAC_EEPROM_REGISTER );
  int failed = Wire.endTransmission();
  if( failed )
  {
    Serial.printf( "Unable to retrieve MAC address; endTransmission returned %d\n", failed );
    HANG;
  }

  byte* b = addr;
  int bytesRead = Wire.requestFrom( MAC_EEPROM_I2C_ADDRESS, 6 );
  if( bytesRead < 6 )
  {
    Serial.printf( "Unable to retrieve MAC address; fewer than six bytes\n" );
    HANG;
  }

  while( Wire.available())
  {
    *b++ = Wire.read();
  }
}

```

# Answer
Turned out to be a hardware problem.

The PoE FeatherWing's default CS pin conflicts with the MusicMaker's DCS pin. The PoE CS pin can be changed by cutting a trace on the back of the board and bodging a wire from a pad to whatever pin you want to use instead. I had done that.

Turns out though, that I hadn't cut it completely. This was causing the Ethernet library to pull the MusicMaker DCS low every time it pulled the Ethernet CS low.

I finally figured it out when I hooked up a logic analyzer to all of the CS pins and the SPI pins. It was very clear that MusicMaker DCS was going low with Ethernet CS.

Yay Logic Analyzer.
> 0 votes

---
Tags: adafruit, ethernet-shield
---


# Question
Title: Do I need to use resisters and ICes for specific sensors.What if I dont use it,Will it impact my arduino board some how like making it useless &faulty
So I was reading about `Simple AutomaticLight Controller using LDR.` or with the help of LDRSenso. But I have `LM393 Photoresistor (LDR) Sensor` I like to know are they same thing or somewhat different. So the way they work is detecting

`light intensity and It is associated with both analog output pin and digital output pin labelled as AO and DO respectively on the board.When there is light, the resistance of LDR will become low according to the intensity of light. The greater the intensity of light, the lower the resistance of LDR.`

And before that I was reading about `Analog Piezoelectricity Ceramic Piezo Vibration Sensor For Arduino`

So LDR sensor needs some kind of resister. And Analog Piezoelectricity Ceramic Vibration sensor needs some IC.

Can Anyone be specific to these specific resister and IC for each sensor types. I have them both as module for arduino Uno. I like to code in gcc avr. Thats how I am restricted. And will use C code to control but Can any please be briefly explain this type of resister for LDR sensor and Piezoelectric vibration sensor IC. Just few words to help me relate to some explanation and easily get poped in my mind when ever I hear about these two modules again. So Just some brief on this specific types of resister and IC related to these two modules why they needed how to code, and control through code

These are pictures of two sensors as Chrisl asked

https://www.daraz.pk/products/i140816069-s1300868385.html?urlFlag=true&mp=1

https://www.daraz.pk/products/i162290793-s1329534669.html?urlFlag=true&mp=1

# Answer
> 2 votes
Both images, that you included, show breakout boards. They are circuit boards (PCB - Printed Circuit Board), which contain all the components, that are needed to connect the corresponding part (in your case a piezo and an Light Dependent Resistor (LDR)). They are called this way, because they are "breaking out" simple output pins from the sensor circuitry for directly connecting them to the Arduino. So instead of having to build the circuit with the pure components you have a simple board, which gives you standard pin headers for connecting to the your Arduino. They are also often called "modules".

That means, that you don't need any additional circuitry or ICs to connect these boards to the Arduino. For the piezo sensor the image even shows the wiring diagram. You can see there, that you don't need any additional parts besides the breakout board, piezo and the Arduino itself. The same applies for the LDR breakout board.

**Connections:**

For the piezo sensor you have an analog output. Thus you need to connect that to an analog input on the Arduino (A0 to A7) and read the signal with `analogRead()`.

For the LDR breakout board I cannot be sure without a link to the product page, but it seems, that it has a digital output (DO). You can connect it to a digital input of the Arduino. It will switch between HIGH and LOW at a certain threshold brightness, which you can control by using a small screwdriver to rotate the little potentiometer on the board (the blue box with the hole in the middle).

---
Tags: arduino-uno, sensors, c, avr-gcc
---


# Question
Title: Can't figure out why what I'm trying to do isn't working. Trying to light up a RGB with buttons with AnalogWrite
DISCLAIMER : I have just started Arduino, like 2 days ago, I've been following some tutorials on YouTube, and I tried to do this myself, but I can't figure out why it isn't working, can't find what I'm doing wrong.

THE SETUP (I've already tested it with some other code that proved me that the setup is working, each buttons and each pin of the RGB LED, I know for sure that this is the code not working)

I am trying to light up the led to RED thanks to second button (from LEFT), Green with third, Blue, with fourth, and the first one to put the LED off . I am aswell trying to make a perfect visual transition between each colors going from 255 to 0 decimal..

Here is my code :

```
#define BLUE 10
#define GREEN 11
#define RED 5

int buttonBlueOn = 9;
int buttonGreenOn = 8;
int buttonRedOn = 3;
int buttonRgbLedOff = 2;

void setup() {
  // put your setup code here, to run once:
  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
  pinMode(buttonRedOn, INPUT_PULLUP);
  pinMode(buttonGreenOn, INPUT_PULLUP);
  pinMode(buttonBlueOn, INPUT_PULLUP);
  pinMode(buttonRgbLedOff, INPUT_PULLUP);

}

int redValue;
int greenValue;
int blueValue;

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

  // -------------------------------- RED ------------------------------ //

#define delayTime 20

  if (digitalRead(buttonRedOn) == LOW)
  {
    digitalWrite(RED, HIGH);
    redValue = 255;
  }

  if (digitalRead(buttonRedOn) == LOW and GREEN == HIGH)
  {
    for (int i = 0; i < 255; i += 1) // fades out green brings red full
    {
      redValue += 1;
      greenValue -= 1;
      analogWrite(RED, redValue);
      analogWrite(GREEN, greenValue);
      delay(delayTime);
    }
  }

  if (digitalRead(buttonRedOn) == LOW and BLUE == HIGH)
  {
    for (int i = 0; i < 255; i += 1) // fades out blue brings red full
    {
      redValue += 1;
      blueValue -= 1;
      analogWrite(RED, redValue);
      analogWrite(BLUE, blueValue);
      delay(delayTime);
    }
  }

  // -------------------------------- GREEN ------------------------------ //

  if (digitalRead(buttonGreenOn) == LOW)
  {
    digitalWrite(GREEN , HIGH);
    greenValue = 255;
  }

  if (digitalRead(buttonGreenOn) == LOW and RED == HIGH)
  {
    for (int i = 0; i < 255; i += 1) // fades out red brings green full
    {
      greenValue += 1;
      redValue -= 1;
      analogWrite(RED, redValue);
      analogWrite(GREEN, greenValue);
      delay(delayTime);
    }
  }

  if (digitalRead(buttonGreenOn) == LOW and BLUE == HIGH)
  {
    for (int i = 0; i < 255; i += 1) // fades out blue brings green full
    {
      greenValue += 1;
      blueValue -= 1;
      analogWrite(BLUE, blueValue);
      analogWrite(GREEN, greenValue);
      delay(delayTime);
    }
  }

  // -------------------------------- BLUE ------------------------------ //

  if (digitalRead(buttonBlueOn) == LOW)
  {
    digitalWrite(BLUE, HIGH);
    blueValue = 255;
  }

  if (digitalRead(buttonBlueOn) == LOW and RED == HIGH)
  {
    for (int i = 0; i < 255; i += 1) // fades out red brings blue full
    {
      blueValue += 1;
      redValue -= 1;
      analogWrite(RED, redValue);
      analogWrite(BLUE, blueValue);
      delay(delayTime);
    }
  }

  if (digitalRead(buttonBlueOn) == LOW and GREEN == HIGH)
  {
    for (int i = 0; i < 255; i += 1) // fades out green brings blue full
    {
      blueValue += 1;
      greenValue -= 1;
      analogWrite(BLUE, blueValue);
      analogWrite(GREEN, greenValue);
      delay(delayTime);
    }
  }

  // -------------------------------- OFF ------------------------------ //

  if (digitalRead (buttonRgbLedOff) == LOW)
  {
    digitalWrite(GREEN, LOW);
    digitalWrite(RED, LOW);
    digitalWrite(BLUE, LOW);
  }

}

```

WHAT IT DOES : Nothing at all, nothing lights up, nothing happens, I'm a bit confused

I am so new to this, sorry if I'm doing something wrong on this forum

# Answer
> 2 votes
Your program makes things too complex. You should rethink your whole strategy.

My preferred method is to have a "target" colour and a "current" colour. When you press a button you simply change what your target colour is. And then every iteration through `loop()` (either slowed down with a short `delay()` or triggered with `millis()`) you compare the target and current colours, and if they differ then move the current colour a step closer to the target colour.

That way you remove all the logic of "if this is pressed but not this then fade between these two colours", instead it's just "if this is pressed then I want to go to this colour" and the changing of the colour is completely independent.

I wrote a blog post on the topic some time ago that you might find helpful.

---
Tags: led, button
---


# Question
Title: Changing I2C Address on Garmin LidarLite, resets on reboot
I am trying to change the I2C address of multiple Garmin LidarLite-V3. I am able to change them one at a time using the LidarLite C++ library using the setI2Caddr function, and can see the updated I2C address using i2cdetect. The problem is that when the LidarLite loses power and turns back on, the address is reset to the default 0x62. This means when I have multiple LidarLite connected in parallel, they all default to the same I2C address and I am unable to individually change the addresses using the setI2Caddr function. How can I change the I2C address so that it persists through a power cycle, or alternatively change the addresses when multiple LidarLite are connected in parallel?

# Answer
The manual of the Garmin Lidar Lite tells you about this. From page 5 under "Configurable I2C Address":

> This can be used to run multiple devices on a single bus, by enabling one, changing its address, then enabling the next device and repeating the process.
>
> The I2C address will be restored to default after a power cycle

So you cannot change the fact, that the I2C addresses will be reset to the default value on power cycle. But on startup you can give each sensor its own address. For that you need to wire the Power Enable pins to digital pins on the Arduino. When the digital pin is HIGH, the sensor is deactivated. So you start with all the Power Enable pins on HIGH. Then you set the first Power Enable pin to LOW, set the sensors I2C address via the corresponding I2C command and then proceed to the next sensor. When finished every sensor has its corresponding address and you don't get any collisions on the bus, since you always have only one sensor with the default address enabled at any time.
> 2 votes

---
Tags: i2c
---


# Question
Title: ESP32 playing wav sounds distorted or too fast (but not all wav's)
I am building a sound project on an ESP 32 via I2S using an example from: https://github.com/nhatuan84/esp32-i2s-sdcard-wav-player

It works fine with the example wav file t.wav posted in the repository.

But when I try to use a custom wav which I exported from audacity it sounds distorted and/or too slow/fast (it sounds fine when I play it within audacity).

This is an example that is totaly distorted when playing from the esp with I2S: badly sounding wav While it sounds fine when playing from audacity.

I tried different settings in audacity exporting 16 bit, 32 bit. 44100 Hz, 11025.

Any hints or tips what I could try or am doing wrong?

Thx!

Jeroen

# Answer
> 2 votes
I know this is an old question, but I ran into the same issue and spent days trying to figure it out, so I hope this answer can save another poor soul from that:

## The Problem

For reference, I have an ESP32 (HUZZAH32 from Adafruit) hooked up to a breakout board that uses a PCM5102a chip as an external DAC. I would set the sample rate to 44.1khz but it seemed my song, which I confirmed had accurate data, would play at double speed. Lowering the sample rate to 22.05khz made it play at the correct speed, but quality seemed reduced.

## The Root Cause

The issue was that my .channel\_format value was set to I2S\_CHANNEL\_FMT\_RIGHT\_LEFT, however *my data was mixed to mono in Audacity.* This problem may exhibit itself differently depending on how you push bytes via I2S, but for me, this meant double speed. This is because the protocol was reading every 2 samples (which should be separate) as the left/right channels. Effectively, this meant that it was skipping every other sample, which doubled the playback speed.

## Code Snippets

My i2s\_config\_t code:

```
#define DMA_BUFFER_COUNT 8
#define DMA_BUFFER_LENGTH 1024

i2s_config_t i2s_config = {
    .mode = I2S_MODE_MASTER | I2S_MODE_TX,
    .sample_rate = 44100,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
    .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // this was causing double speed due to being I2S_CHANNEL_FMT_RIGHT_LEFT
    .communication_format = (i2s_comm_format_t) (I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
    .tx_desc_auto_clear = true,
    .dma_buf_count = DMA_BUFFER_COUNT,
    .dma_buf_len = DMA_BUFFER_LENGTH,
    .use_apll = false,
    .intr_alloc_flags = 0
};

```

My code for sending bytes to the external DAC (which is its own task):

(rx\_buffer and readPointer here are part of a ring buffer that I streamed in over wifi on a separate task, please only use as reference and substitute for your own sound data)

```
int FRAMES_TO_SEND = 1024;
int32_t frames[FRAMES_TO_SEND];

for (int i = 0; i < FRAMES_TO_SEND; i++) {
    // copy into i2s buffer
    int32_t sample = (int32_t)rx_buffer[readPointer];

    // write bytes to the i2s peripheral
    esp_err_t err = i2s_write(i2s_num, (char *) &sample,
        sizeof(int32_t), &bytesWritten, 100);

    if(err != ESP_OK) {
        printf("ESP32 i2s Errorcode %i", err);
    }

    readPointer++;
    if (readPointer == TOTAL_BUFFER_SIZE) {
        readPointer = 0;
    }
}

```

Hopefully this helps out some of you, or at least get you on track to the solution. Unfortunately, the I2S protocol on the ESP32 is not very well documented so there's a lot of trial and error to be had. Keep at it!

---
Tags: esp32, sound
---


# Question
Title: Why use multiple filters output thousands value, use one or two filter can output normal value?
## Problem

If the loop runs only one simulated pin and uses a filter, it will output normally (analog 1600~2200, filter 0~500), but creating an array to store multiple filtered simulated pin values will cause problems, and the output will be abnormal (10000~80000).

If analog read six sensors, and their value is 1600~2200.

It used the code below.

```
#include "EMGFilters.h"
#define SERIAL_TX_BUFFER_SIZE 4096 //修改串口接收缓冲区大小为2048
const int SensorInputPin[] = {A0, A1, A2, A3, A4, A5};
EMGFilters myFilter[6];
SAMPLE_FREQUENCY sampleRate = SAMPLE_FREQ_500HZ;
NOTCH_FREQUENCY humFreq = NOTCH_FREQ_50HZ;

void setup()
{

    for (int i = 0; i < 6; i++)
    {
        myFilter[i].init(sampleRate, humFreq, true, true, true);
    }
    Serial.begin(115200);
    Serial1.begin(250000);
    analogReadResolution(12);
}
void loop()
{
    long time = millis();
    int x[6] = {0,0,0,0,0,0};
    int analog[6] = {0,0,0,0,0,0};

    for (int index = 0; index < 6; index++)
    {
        //x[index] = myFilter[index].update(analogRead(SensorInputPin[index]));
        analog[index] = analogRead(SensorInputPin[index]);
    }
    while ((millis() - time) < 2)
    {
        delayMicroseconds(100);
    }
    Serial.printf("filter:%d %d %d %d %d %d analog:%d %d %d %d %d %d\n", x[0], x[1], x[2], x[3], x[4], x[5],analog[0],analog[1],analog[2],analog[3],analog[4],analog[5]);
    Serial1.printf("<%d,%d,%d,%d,%d,%d>", x[0], x[1], x[2], x[3], x[4], x[5]);
}

```

Change `index<6` to `index<2`, and uncomment `\\x[index] = myFilter[index].update(analogRead(SensorInputPin[index]));`

It will be a normal output using two inputs and filters.

But changing `index=0;index<2` to `index=0;index<6` will be abnormal output.

If changing to `index=3;index<4` or `index=2;index<3`...etc, it will be normal output:

Sketch:

XIAO MCU schematic in the HTML: https://files.seeedstudio.com/wiki/Seeeduino-XIAO/res/Seeeduino-XIAO-v1.0-SCH-191112.pdf

I don't have a sensor schematic.

Filter code: https://github.com/oymotion/EMGFilters/blob/master/EMGFilters.cpp

I hope each simulation output to look like the first Gif(normal output), what should I do to achieve this?

# Answer
This is a bug in the filter library you are using. It has already been reported (by you, maybe?).

As Majenko and chrisl suggested in comments, the crux of the problem is that you need one filter per input channel. Using multiple instances of `EMGFilters` seems like the obvious solution. Alas, the library keeps the filter state in global variables, which essentially means your six filter objects all reference the same actual filter.
> 2 votes

---
Tags: sensors, c, hardware, analog
---


# Question
Title: ESP32 BLE service not visible to Node-RED
I've been trying to host a BLE-server on my ESP32 with a custom service. I'm trying to read the service data in NodeRED, but the service IDs and custom characteristics don't show up in NodeRED whenever I import the scan results of my BLE input node in NodeRED. The device and service both show up fine on Android using NRFtool:

However, in NodeRED, the device shows up and connects just fine, but none of the characterestics are displayed at NodeRED's end:

I'm using NodeRED version 1.2.9 with the Generic BLE node. And the ESP32 sample i'm using can be found here.

I've also tried the BLE server examples included with the BLEDevice library with similar issues. In addition to this: I'm not sure if this is just a typo in the node's error message, or if it's a typo in the node itself, but all mentions of the type "topic" are displayed as "tpoic" in NodeRED's debug log. Though i doubt this affects the functionality of the node in any way. Nowhere in my flow is there a mention of the keyword "tpoic".

Any suggestions as to how i could make my services show up in NodeRED would be appreciated. Thank you.

# Answer
> 0 votes
Turns out BlueZ on linux no longer supports ESP32 BLE devices. I tried the same code on a bluefruit M0 and it worked just fine.

---
Tags: bluetooth, esp32, ble
---


# Question
Title: Which Arduino board would be sufficient for the following simulink project?
I'm trying to run a Fuzzy Logic MPPT Algorithm in Arduino UNO, here's the Simulink File

However when I try to upload the code, I get the following error

Is this project possible with UNO, if yes, what shall I do? Else which Arduino board would be suitable for this project?

# Answer
> 1 votes
We cannot answer about your Simulink program (since this site is only about Arduino). But currently you are using 3 times the RAM an Uno has. You can try to optimize your Simulink program, though I doubt that you will be able to reduce the needed memory that much.

So you should switch to a board with significantly more RAM. There are many to choose from, with different RAM, flash, peripherals, pin numbers. Write down all your requirements and then look for a fitting board

---
Tags: arduino-uno, memory-usage, solar
---


# Question
Title: How to know the data type of constant in arduino IDE?
For example constant of `MSBFIRST`, It used to as input in `SPI.setBitOrder(MSBFIRST)`. But sadly i don't know the type data of `MSBFIRST`. This is applied to like `SPI_MODE0` and other.

# Answer
From SPI.h:

```
#ifndef LSBFIRST
#define LSBFIRST 0
#endif
#ifndef MSBFIRST
#define MSBFIRST 1
#endif

```

As you can see, there is no type. The compiler merely substitutes literally "1" for "MSBFIRST". So your code would read, after pre-processing:

```
 SPI.setBitOrder(1)

```

To know exactly what type "1" is (I'm not sure why you need to know) then you would look up "C++ integer promotion". In other words, how are literal integers handled by the C++ compiler.

---

From a comment:

> In which case it's better to look at the desired type of the destination function not the type of the macro or const.

Quite right. If you look further into the SPI.h file you see this:

```
  // This function is deprecated.  New applications should use
  // beginTransaction() to configure SPI settings.
  inline static void setBitOrder(uint8_t bitOrder) {

```

So, ignoring what type MSBFIRST is, you can see that the function expects uint8\_t.
> 2 votes

---
Tags: data-type, constants
---


# Question
Title: Arduino Code is slow when running two task
* I want pin 11 to give a square wave of once 3 sec
* and want pin 12 to act as a switch to glow pin 13 Built in led..
* connected pin 12 to GND with 10k resistor

If i fed the switch code alone it is fast(the led turn on instantly), but when i fed this code (the on in picture) it has a 1 sec lag to turn on the led. why?

And also if i connect pin 11 to 12 i assume the builtin led should turn on and off according to the wave form coming out of but the led is always low.

# Answer
Basically, the code comprises Two Parts

1. Square Wave with 3-sec delay on and 3-sec delay off
2. if the pin 12, is high pin 13 will be high and if 12 is low 13 is also low(a simple on/off switch)

---

The second block of code executes only after the first Block of code is done executing.

That's why when I connected the pin 11 to 12 the led wasn't shining because when the WAVE code(that first block of code which gives a square wave) was running, the second block of code was quiet hence the 12th pin was still not become a switch yet.

And the delay I told was due to this, actually, the delay is 6 sec. Because after the switch code completes its run, it will take a whole six seconds for the Arduino to come back to switch code in the loop because the wave signal of 3 sec on and 3 sec off should run.

if you want to run two tasks at the same time you need to learn the topics of millis() and protothreading.

---

You can ask me why I was this dumb to ask a question like this, because the only programming language I have ever snorted is python and in there I learned python is an interpreter and it executes line by line, Since Arduino ide code is built around c++ which is the compiler.

I thought the code won't run line by line instead the code will be compiled but now only I understood that even though the file is compiled there is no chance every code can be run at the same time as the actual meaning of the compiler.
> 2 votes

---
Tags: arduino-uno
---


# Question
Title: Digispark ATtiny 85 - not recognised as HID device
I am trying to create auto-username-writer with Digispark ATtiny 85. I have a pretty simple sketch:

```
#include <DigiKeyboard.h>
void setup() {
}
void loop() {
    DigiKeyboard.sendKeyStroke(0);
    DigiKeyboard.delay(100);
    DigiKeyboard.sendKeyStroke(76, MOD_ALT_RIGHT | MOD_CONTROL_LEFT); //ctrl+alt+del
    DigiKeyboard.delay(2000);
    DigiKeyboard.print("username");
    DigiKeyboard.sendKeyStroke(0x2B); //tab

    for (;;) {}
}

```

When I upload the code via Arduino IDE into the device (Board: Digispark (Default - 16.5MHz), it finishes successfully, it is recognised as a HID device and immediately performs all the tasks. But when I disconnect it and connect it again (to my PC or any other PC) it is recognised as Digispark Bootloader and does not do anything.

I have tried it in Arduino IDE 1.8.8 and in Arduino IDE 1.6.5 and I have no idea why it is only recognised right after upload. I have two ATtinys and both do the same behaviour.

Can someone help me, please?

EDIT: I tried adding `digitalWrite(1, HIGH);` to detect, whether the sketch even runs and it turned out it does not even run (except after the upload)...

# Answer
After five seconds, which is when the bootloader should switch to running your code, how does it show up in the list of usb devices (`lsusb` in Linux, Device Manager in Windows, etc.)

The original Digispark was a bit temperamental when plugged into USB 3 ports. If you are plugging it into one, try using a usb2 hub and plugging it into the hub instead.

Also, did you get this from Digistump, or is it a knock-off?
> 1 votes

# Answer
I hope I'm not too late here, but I believe you may need to update the bootloader. Could you try the instructions here? https://www.iot-experiments.com/micronucleus-bootloader-upgrade/ I happened to be reading through this guide: https://hackernoon.com/low-cost-usb-rubber-ducky-pen-test-tool-for-3-using-digispark-and-duck2spark-5d59afc1910 and the troubleshooting section there sounds like the issue you're having.
> 1 votes

# Answer
Same here,

I am new user so not saying this is the correct way.

I now includes "digiKeyboard.update();" and "digiKeyboard.delay(1000);". both at top ,both to trigger usb signal many many times to the host.

the "digiKeyboard.sendKeyStroke(0);" does not work every time.
> 0 votes

---
Tags: attiny, keyboard, attiny85, digispark
---


# Question
Title: How arduino PORTB pins are addressed if I want to access them with pointer?
So Let say I want to access Digital Pin 13. Thats port B pin. Is there an memory address associated with this pin 13 of Arduino Uno r3?

How Port B pins are addressed with corresponding pin numbers like What is the address of digital pin 13?

Currently I am assuming its pin address is `0x05` so my pointer looks like this

```
    uint8_t *adr=0x00+0x05;

```

Now If I do

```
  *adr |= 0x20;

```

then should I expect that I am writing to some pin so I like to know which data sheet I should refer for pin addresses of port B?

Currently my code is not working because no matter which pin I connect my LED to it blinks. What if I dont want to use any library and just write to address and I need PORT B pin address how to refer to different pins?

Now assume I have a code that uses avr gcc

```
#include <avr/iom328p.h>//avr/io.h
#include    <util/delay.h>
#include <avr/sfr_defs.h>

#define     BLINK_DELAY_MS  18000

int main (void)
{
    DDRB |= _BV(DDB5);

    while(1)
    {
        PORTB |= _BV(PORTB5);
        _delay_ms(BLINK_DELAY_MS);

        PORTB &= ~_BV(PORTB5);
        _delay_ms(BLINK_DELAY_MS);
    }

    return 0;
}

```

What is the values correspond to `DDRB` and `_BV(DDB5);` and `PORTB` and `_BV(PORTB5);`? I like to do it with pointers so I need addresses of PORT B all pins I know the macros but I like the actual addresses . there should be a some datasheet for this what its called. Does arduino has it that gives memory mapped addresses?

The above code is simply for blinking LED light

# Answer
The address of `PORTB` can be found in the ATmega328P datasheet, section “I/O-Ports”, subsection “Register Description”. It is repeated near the end of the section “Register Summary”, where it is documented as:

```
0x05 (0x25)

```

Admittedly, this is confusing: there are two addresses! There is a footnote that gives some hint on the reason:

> When using the I/O specific commands IN and OUT, the I/O addresses 0x00 - 0x3F must be used. When addressing I/O Registers as data space using LD and ST instructions, 0x20 must be added to these addresses.

To clarify, this register is mapped in two different address spaces:

* It has I/O address `0x05`. The I/O address space is a small (6 bits) address space dedicated to I/O registers. It is actually too small to host all the I/O registers. It can be accessed with the `in` and `out` instructions. Addresses lower than 32 can also be bit-addressed with the instructions `sbi`, `cbi`, `sbis` and `sbic`.
* It has data address `0x25`. This is a much larger address space that gives access to all the I/O registers and to the RAM. It is used with the `ld`/`st` family of instructions.

Since the C and C++ languages are not aware of the I/O address space, a regular data pointer is assumed to always point to the data address space. Thus, if you want to create a pointer to this register, you may write:

```
volatile uint8_t *adr = 0x25;  // PORTB

```

Note the `volatile` qualifier. This is required to prevent the compiler being too “smart” and optimizing out some of your writes under the assumption that this is just RAM.

Then you may turn the LED on with:

```
*adr |= 1 << 5;  // set PORTB5

```

Withe the proper optimization options, the compiler may be able to optimize this into `sbi 0x05, 0x05`, if it can prove that the address stored in the pointer doesn't change.

> I need addresses of PORT B all pins I know the macros but I like the actual addresses.

I would recommend this:

```
volatile uint8_t *adr = &PORTB;

```

This avoids having magic numbers in your code, and makes the programmer's intent clear.

One last comment. When compiling your code I got:

```
include/avr/iom328p.h: [...]
error "Include <avr/io.h> instead of this file."

```

Just include `<avr/io.h>`: this header takes care of including both the correct `<avr/iomXXX.h>` and `<avr/sfr_defs.h>`.
> 6 votes

---
Tags: arduino-uno, led, c
---


# Question
Title: Working with two analog sensors, ATmega16
I want to work with two analog sensors connected to ATmega16. The first one is the temperature sensor (LM35), connected to PA0. The LED should be ON when the temperature goes high. The second one is the photo-resistor and it's connected to PA1. I want to turn the LED ON when the lighting is low. I was looking for a solution all over the internet, but couldn't find something similar to my situation. They all just say one thing- you can't read two analog sensors at the same time.

I have this code that works just for one sensor:

```
#define F_CPU 1600000UL
#include <avr/io.h>

void ADC_Init() {
    DDRA = 0x0;
    ADCSRA = 0x87;
    ADMUX = 0x40;
}

uint16_t ADC_Read(uint8_t channel) {
    ADMUX = (ADMUX & 0xF0) | (channel & 0x0F);
    ADCSRA |= (1 << ADSC);
    while (ADCSRA & (1 << ADSC)) ;
    return ADC;
}

int main(void) {
    ADC_Init();
    DDRB = 0xFF;
    while (1) {
        uint8_t value;
        value = ADC_Read(0);
        if (value > 70) {
            PORTB |= (1<<0);
        } else {
            PORTB &= ~(1<<0);
        }
    }
}

```

Can you somehow help me to modify this code, give me advice on how can I do it, or some example?

# Answer
> 0 votes
I just skimmed over the ATmega16 datasheet, so perhaps take this answer with a grain of salt.

It is technically true that you cannot read two ADC channels at the same time because there is only one ADC and you must multiplex the various channel inputs to that one ADC. You can, most likely, change the ADC source at runtime to switch between the different channels.

Take a look at the ADMUX register description in the datasheet

Right now, you set the mux once during the init() function and then never change it. In your new code, you will have to change the value in the register at runtime (in addition to any other configuration/housekeeping that is required) which should happen in your main loop()

High level description:

```
int main()
{
    system_init();

    while (1)
    {
        set ADMUX to read from PA0
        read ADC

        set ADMUX to read from PA1
        read ADC

        do stuff
    }

}

```

---
Tags: avr, atmega
---


title: Arduino Wire library and Type Cast help

text:
# Question
Title: Arduino Wire library and Type Cast help
I'm having troubles understanding this line of code that I see with every Arduino EEPROM tutorial.

```
int saveLocation = 4095;
Wire.write((int)(saveLocation >> 8));

```

*Question 1:* Do the parenthesis indicate that the bitshift happens BEFORE the type cast? What's the order of operations?

According to the wire library documentation, you can use `wire` to send the following:

```
Wire.write(value) // value to send as a single byte
Wire.write(String) // a string to send as a series of bytes
Wire.write(data, length) // an array of data to send as bytes, the number of bytes to transmit.

```

So my variable, `int saveLocation = 4095` is not a string or array, so it must be a considered as a 'value', which means it's being sent as 1 byte.

*Question 2:* If it's being sent as 1 byte, why would I cast it to an integer of 2 bytes?
*Question 3:* If `saveLocation` is already an integer, why would cast it to `int`?
*Question 4:* Since `int` is two bytes, does `wire.write` only send out the lower byte?

Thanks in advance for the help.

# Answer
You are right about being confused. This casting doesn't make any sense.

> Question 1: Do the parenthesis indicate that the bitshift happens BEFORE the type cast? What's the order of operations?

Yes, the bitshift happens before the casting because of the parentheses.

> Question 2: If it's being sent as 1 byte, why would I cast it to an integer of 2 bytes?

You wouldn't. It is cast to `uint8_t` (aka `byte`) anyway by the parameter type.

> Question 3: If saveLocation is already an integer, why would cast it to int?

Doesn't make sense to me either. You don't need to. Though it also doesn't hurt here (same goes for question 2)

> Question 4: Since int is two bytes, does wire.write only send out the lower byte?

Yes, only the lower byte. The function parameter is of type `uint8_t`, which is exactly one byte. Thats the reason for the bit shifting. By shifting the value right by 8 you effectively shift the high byte to the low byte position.
> 2 votes

# Answer
> Question 1: Do the parenthesis indicate that the bitshift happens BEFORE the type cast? What's the order of operations?

Yes. What's in the brackets is evaluated then the result cast to int.

> Question 2: If it's being sent as 1 byte, why would I cast it to an integer of 2 bytes?

You wouldn't.

> Question 3: If saveLocation is already an integer, why would cast it to int?

You wouldn't

> Question 4: Since int is two bytes, does wire.write only send out the lower byte?

Yes, just the lowest 8 bits.

The cast to an integer is completely redundant here. It's not performing anything other than making the code harder to understand. You can remove it and it won't make any difference.

In fact the presence of the int cast triggers an overloaded function:

```
inline size_t write(int n) { return write((uint8_t)n); }

```

So it just re-casts it to an unsigned 8-bit integer (byte) anyway.
> 1 votes

---
Tags: i2c, wire-library
---


title: Pelco D Library for Arduino

text:
# Question
Title: Pelco D Library for Arduino
Does anyone know of any Pelco D libraries available for Arduino?

I already have a PTZ joystick controlling 3 PTZ cameras, but I would like to add an Arduino 'intelligent' controller capable of sending automated sequences of Pelco D commands according to various PIR triggers etc.

# Answer
It looks like someone wrote a Pelco library. Check out:

https://github.com/Pixelbo/Pelco\_And\_Arduino
> 2 votes

---
Tags: programming, communication, rs485
---


# Question
Title: ESP32: why does malloc can only alloc 126888 bytes with 402540 bytes heap?
I need a buffer of the size `153600 bytes` in my ESP32 project, but I could not create a static buffer in code nor can I `malloc` the buffer.

The ESP32 itself reports:

```
  ESP.getHeapSize()  // = 402540
  ESP.getFreeHeap()  // = 376980
  ESP.getPsramSize() // = 0
  ESP.getFreePsram() // = 0

```

`malloc(153600)` returns `NULL`; I wrote a little loop which decrements by one and retries to malloc. The first time it works is `malloc(126888)` so this seems to be the maximum buffer I can get.

*(I tried it with different boards; one from eBay and one from a genuine distributor - no differences.)*

Clearly the heap size is big enough. So I'm thinking that the RAM is possibly split into different chunks and no one is big enough for my buffer?

Is there any documentation which explains this behavior?

# Answer
Yes, the ESP32 memory is arranged in a number of different chunks. Those chunks are all aggregated by the memory manager to appear as a single heap, but they're actually separate.

As the ESP-IDF documentation mentions:

> At startup, all ESP-IDF apps log a summary of all heap addresses (and sizes) at level Info:
>
> ```
> I (252) heap_init: Initializing. RAM available for dynamic allocation:
> I (259) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
> I (265) heap_init: At 3FFB2EC8 len 0002D138 (180 KiB): DRAM
> I (272) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
> I (278) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
> I (284) heap_init: At 4008944C len 00016BB4 (90 KiB): IRAM
>
> ```

And of course you have some heap fragmentation from other threads allocating and deallocating memory, so the biggest contiguous block of memory you can allocate will be smaller than the biggest region.

To further illustrate I found this handy memory map on the Arduino forums:

There you can clearly see the SRAM is split into different chunks. The main reason for this is to better allow concurrent access by the two cores in the dual-core variants of the chip.
> 1 votes

---
Tags: arduino-ide, esp32, espressif
---