# Question
Title: How to build an µC controlled power regulator?
The idea: Use an Arduino-like µC to control a set of CPU fans by control the voltage the CPU fans receive. My question: How can I do this in an easy way?

Yes, I know, there is PWM but this is not very suitable for this purpose: I did some tests with it but it turns out that the characteristics of PWM fans don't suite my purpose: PWM fans are designed to be controlled between not audible and full speed, not between nearly not rotating and full speed. Even very special fans with a wide range of PWM speed control don't work as well as powering a regular 12V fan with just 5 or 6V.

Which brings me to an alternative idea: It should be possible to send an I2C or SPI signal to some kind of IC that then provides a voltage as required.

Alternatively to that there seem to be PWM controlled voltage regulators available. But that doesn't seem to be a very elegant solution, isn't it?

Or is there an even better way of controlling the voltage output to the fans I'm not aware of?

I'm aiming for a solution that works well and can be build with little effort. Unfortunately I don't know how to do that: I can't find any breakouts I could use right away for that purpose. Probably there are some and I just have not been able to find them? Any help is appreciated!

# Answer
It's actually very simple, and it indeed does involve using PWM.

PWM is just a signal being on for a percentage of a predefined period. That percentage is also, when the PWM signal is averaged out, equivalent to a percentage of the peak voltage of the PWM signal.

So if you take a PWM signal and use it to switch a higher power source (say, through a P-channel MOSFET that's being itself switched by an N-channel MOSFET), then add smoothing to that switched output, and you have a variable voltage.

If you want to know what that voltage is you can add a resistor network to the output (assuming it's higher than the voltage the MCU can handle) and read the divided voltage using an ADC. You can then use that as a feedback loop to adjust the PWM duty cycle to get your desired voltage.

simulate this circuit – Schematic created using CircuitLab

That is precisely\* how switch-mode power supplies work. Use PWM to switch a FET that controls an incoming voltage, then smooth it out, adding a feedback network for control.

---

\*Apart from the PWM bit: they often use other methods of switching the signal to get better efficiency at different loads, such as PFM.
> 3 votes

---
Tags: voltage-level, voltage
---


# Question
Title: How can I call the constructor of a Class after it called once?
I have a problem with Classes. I have a bunch of code, that code is uses Adafruit MQTT FONA library. I call the Class for make new Instance and Connect to specified server with this code;

```
Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

```

When I want to connect to another server, I call this Instance's constructor after it called once. But it don't let me to construct it again. How can I construct the same instance again after it constructed once?

```
void connectMqtt()
{
    Adafruit_MQTT_FONA mqtt(&fona, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
}

connectMqtt();

```

Thanks for helps! Have a good day!

# Answer
> 2 votes
You can't. Once constructed, an object remains constructed until you delete it.

You could either use dynamic allocation and `new` and `delete` to create and destroy objects at will, or you could create multiple objects with different settings and use different objects at different times.

Another option would be to modify the library to create a new function that accepts and assigns new server settings. All the constructor does is store the settings in variables - there's no reason why you couldn't just change those variables.

---
Tags: c++, adafruit, mqtt
---


title: Determining contrast on LCD

text:
# Question
Title: Determining contrast on LCD
So I am using this code here and I do get text written on my LCD screen. Problem is that text is almost invisible. I tried with background light on or off and it did not help. LCD is connected to 5v and backlight is connected to 3.3V. Backlight is connected through potentiometer.

How to control text contrast? To have more contrast?

```
#define PIN_SCE   7
#define PIN_RESET 6
#define PIN_DC    5
#define PIN_SDIN  4
#define PIN_SCLK  3

#define LCD_C     LOW
#define LCD_D     HIGH

#define LCD_X     84
#define LCD_Y     48

static const byte ASCII[][5] =
{
 {0x00, 0x00, 0x00, 0x00, 0x00} // 20
,{0x00, 0x00, 0x5f, 0x00, 0x00} // 21 !
,{0x00, 0x07, 0x00, 0x07, 0x00} // 22 "
,{0x14, 0x7f, 0x14, 0x7f, 0x14} // 23 #
,{0x24, 0x2a, 0x7f, 0x2a, 0x12} // 24 $
,{0x23, 0x13, 0x08, 0x64, 0x62} // 25 %
,{0x36, 0x49, 0x55, 0x22, 0x50} // 26 &
,{0x00, 0x05, 0x03, 0x00, 0x00} // 27 '
,{0x00, 0x1c, 0x22, 0x41, 0x00} // 28 (
,{0x00, 0x41, 0x22, 0x1c, 0x00} // 29 )
,{0x14, 0x08, 0x3e, 0x08, 0x14} // 2a *
,{0x08, 0x08, 0x3e, 0x08, 0x08} // 2b +
,{0x00, 0x50, 0x30, 0x00, 0x00} // 2c ,
,{0x08, 0x08, 0x08, 0x08, 0x08} // 2d -
,{0x00, 0x60, 0x60, 0x00, 0x00} // 2e .
,{0x20, 0x10, 0x08, 0x04, 0x02} // 2f /
,{0x3e, 0x51, 0x49, 0x45, 0x3e} // 30 0
,{0x00, 0x42, 0x7f, 0x40, 0x00} // 31 1
,{0x42, 0x61, 0x51, 0x49, 0x46} // 32 2
,{0x21, 0x41, 0x45, 0x4b, 0x31} // 33 3
,{0x18, 0x14, 0x12, 0x7f, 0x10} // 34 4
,{0x27, 0x45, 0x45, 0x45, 0x39} // 35 5
,{0x3c, 0x4a, 0x49, 0x49, 0x30} // 36 6
,{0x01, 0x71, 0x09, 0x05, 0x03} // 37 7
,{0x36, 0x49, 0x49, 0x49, 0x36} // 38 8
,{0x06, 0x49, 0x49, 0x29, 0x1e} // 39 9
,{0x00, 0x36, 0x36, 0x00, 0x00} // 3a :
,{0x00, 0x56, 0x36, 0x00, 0x00} // 3b ;
,{0x08, 0x14, 0x22, 0x41, 0x00} // 3c <
,{0x14, 0x14, 0x14, 0x14, 0x14} // 3d =
,{0x00, 0x41, 0x22, 0x14, 0x08} // 3e >
,{0x02, 0x01, 0x51, 0x09, 0x06} // 3f ?
,{0x32, 0x49, 0x79, 0x41, 0x3e} // 40 @
,{0x7e, 0x11, 0x11, 0x11, 0x7e} // 41 A
,{0x7f, 0x49, 0x49, 0x49, 0x36} // 42 B
,{0x3e, 0x41, 0x41, 0x41, 0x22} // 43 C
,{0x7f, 0x41, 0x41, 0x22, 0x1c} // 44 D
,{0x7f, 0x49, 0x49, 0x49, 0x41} // 45 E
,{0x7f, 0x09, 0x09, 0x09, 0x01} // 46 F
,{0x3e, 0x41, 0x49, 0x49, 0x7a} // 47 G
,{0x7f, 0x08, 0x08, 0x08, 0x7f} // 48 H
,{0x00, 0x41, 0x7f, 0x41, 0x00} // 49 I
,{0x20, 0x40, 0x41, 0x3f, 0x01} // 4a J
,{0x7f, 0x08, 0x14, 0x22, 0x41} // 4b K
,{0x7f, 0x40, 0x40, 0x40, 0x40} // 4c L
,{0x7f, 0x02, 0x0c, 0x02, 0x7f} // 4d M
,{0x7f, 0x04, 0x08, 0x10, 0x7f} // 4e N
,{0x3e, 0x41, 0x41, 0x41, 0x3e} // 4f O
,{0x7f, 0x09, 0x09, 0x09, 0x06} // 50 P
,{0x3e, 0x41, 0x51, 0x21, 0x5e} // 51 Q
,{0x7f, 0x09, 0x19, 0x29, 0x46} // 52 R
,{0x46, 0x49, 0x49, 0x49, 0x31} // 53 S
,{0x01, 0x01, 0x7f, 0x01, 0x01} // 54 T
,{0x3f, 0x40, 0x40, 0x40, 0x3f} // 55 U
,{0x1f, 0x20, 0x40, 0x20, 0x1f} // 56 V
,{0x3f, 0x40, 0x38, 0x40, 0x3f} // 57 W
,{0x63, 0x14, 0x08, 0x14, 0x63} // 58 X
,{0x07, 0x08, 0x70, 0x08, 0x07} // 59 Y
,{0x61, 0x51, 0x49, 0x45, 0x43} // 5a Z
,{0x00, 0x7f, 0x41, 0x41, 0x00} // 5b [
,{0x02, 0x04, 0x08, 0x10, 0x20} // 5c ¥
,{0x00, 0x41, 0x41, 0x7f, 0x00} // 5d ]
,{0x04, 0x02, 0x01, 0x02, 0x04} // 5e ^
,{0x40, 0x40, 0x40, 0x40, 0x40} // 5f _
,{0x00, 0x01, 0x02, 0x04, 0x00} // 60 `
,{0x20, 0x54, 0x54, 0x54, 0x78} // 61 a
,{0x7f, 0x48, 0x44, 0x44, 0x38} // 62 b
,{0x38, 0x44, 0x44, 0x44, 0x20} // 63 c
,{0x38, 0x44, 0x44, 0x48, 0x7f} // 64 d
,{0x38, 0x54, 0x54, 0x54, 0x18} // 65 e
,{0x08, 0x7e, 0x09, 0x01, 0x02} // 66 f
,{0x0c, 0x52, 0x52, 0x52, 0x3e} // 67 g
,{0x7f, 0x08, 0x04, 0x04, 0x78} // 68 h
,{0x00, 0x44, 0x7d, 0x40, 0x00} // 69 i
,{0x20, 0x40, 0x44, 0x3d, 0x00} // 6a j
,{0x7f, 0x10, 0x28, 0x44, 0x00} // 6b k
,{0x00, 0x41, 0x7f, 0x40, 0x00} // 6c l
,{0x7c, 0x04, 0x18, 0x04, 0x78} // 6d m
,{0x7c, 0x08, 0x04, 0x04, 0x78} // 6e n
,{0x38, 0x44, 0x44, 0x44, 0x38} // 6f o
,{0x7c, 0x14, 0x14, 0x14, 0x08} // 70 p
,{0x08, 0x14, 0x14, 0x18, 0x7c} // 71 q
,{0x7c, 0x08, 0x04, 0x04, 0x08} // 72 r
,{0x48, 0x54, 0x54, 0x54, 0x20} // 73 s
,{0x04, 0x3f, 0x44, 0x40, 0x20} // 74 t
,{0x3c, 0x40, 0x40, 0x20, 0x7c} // 75 u
,{0x1c, 0x20, 0x40, 0x20, 0x1c} // 76 v
,{0x3c, 0x40, 0x30, 0x40, 0x3c} // 77 w
,{0x44, 0x28, 0x10, 0x28, 0x44} // 78 x
,{0x0c, 0x50, 0x50, 0x50, 0x3c} // 79 y
,{0x44, 0x64, 0x54, 0x4c, 0x44} // 7a z
,{0x00, 0x08, 0x36, 0x41, 0x00} // 7b {
,{0x00, 0x00, 0x7f, 0x00, 0x00} // 7c |
,{0x00, 0x41, 0x36, 0x08, 0x00} // 7d }
,{0x10, 0x08, 0x08, 0x10, 0x08} // 7e ←
,{0x78, 0x46, 0x41, 0x46, 0x78} // 7f →
};

void LcdCharacter(char character)
{
  LcdWrite(LCD_D, 0x00);
  for (int index = 0; index < 5; index++)
  {
    LcdWrite(LCD_D, ASCII[character - 0x20][index]);
  }
  LcdWrite(LCD_D, 0x00);
}

void LcdClear(void)
{
  for (int index = 0; index < LCD_X * LCD_Y / 8; index++)
  {
    LcdWrite(LCD_D, 0x00);
  }
}

void LcdInitialise(void)
{
  pinMode(PIN_SCE, OUTPUT);
  pinMode(PIN_RESET, OUTPUT);
  pinMode(PIN_DC, OUTPUT);
  pinMode(PIN_SDIN, OUTPUT);
  pinMode(PIN_SCLK, OUTPUT);
  digitalWrite(PIN_RESET, LOW);
  digitalWrite(PIN_RESET, HIGH);
  LcdWrite(LCD_C, 0x21 );  // LCD Extended Commands.
  LcdWrite(LCD_C, 0xB1 );  // Set LCD Vop (Contrast).
  LcdWrite(LCD_C, 0x04 );  // Set Temp coefficent. //0x04
  LcdWrite(LCD_C, 0x14 );  // LCD bias mode 1:48. //0x13
  LcdWrite(LCD_C, 0x20 );  // LCD Basic Commands
  LcdWrite(LCD_C, 0x0C );  // LCD in normal mode.
}

void LcdString(char *characters)
{
  while (*characters)
  {
    LcdCharacter(*characters++);
  }
}

void LcdWrite(byte dc, byte data)
{
  digitalWrite(PIN_DC, dc);
  digitalWrite(PIN_SCE, LOW);
  shiftOut(PIN_SDIN, PIN_SCLK, MSBFIRST, data);
  digitalWrite(PIN_SCE, HIGH);
}

void setup(void)
{
  LcdInitialise();
  LcdClear();
  LcdString("Hello World!");
}

void loop(void)
{
}

```

# Answer
Have you even read your own code? Notably line 141; `LcdWrite(LCD_C, 0xB1 ); // Set LCD Vop (Contrast).`.

Try sending a different value. Note that the first bit should always be 1 so the value should be between 0x80 and 0xFF
> 1 votes

# Answer
`Backlight is connected through potentiometer.`

You should try to turn the potentiometer.

If that doesn't work, there might be something wrong with the connection from the potentiometer to the `Backlight`.

---

Check out this video: https://www.youtube.com/watch?v=XR8ede6V6eA&t=65 Where the led would be replaced with the BL (black-light) pin.
> 0 votes

# Answer
I assume you are using a 16 pin 16x2 display. Try wiring a potentiometer like this with the pot wiper on pin 3. This is what adjusts text contrast:
> 0 votes

# Answer
Same problem, I solved it following this:

> The contrast can be adjusted by the potential-meter displayed in the following picture.

credit
> 0 votes

---
Tags: arduino-uno, lcd
---


# Question
Title: 16*2 1602A LCD not displaying any character or contrast, although correct wiring
I've used my LCD display with a lot of small projects. Now it doesn't work anymore. Most of the diagrams you find online use resistors and potentiometers. However I never done that, but still got it to work nonetheless.

I tried different schematics, but none of them seem to work. So I'm thinking I broke my LCD somehow.

I'm pretty sure the schematic I'm currently using works. The backlight works but nothing is being printed.

Pictures of the project (Warning!! wire hell)

The LCD component is a blue 16x2 1602A v2.0 display. All the components come from the official Arduino starter kit, except for the wires. Though I tested every single wire for continuity, to make absolutely sure the problem was not within the wires themselves. Also, turning the potentiometer doesn't show any blocks, or change anything on the LCD as expected.

Code:

```
#include <LiquidCrystal.h>
//LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  pinMode(6,OUTPUT);
  analogWrite(6,90); //sweetspot for backlight without using a potentiometer.

  lcd.begin(16, 2);
  //lcd.setCursor(0,0);
  lcd.print("hello, world!");
}

void loop() {
}

```

I'm not sure what's wrong, I tested everything I can think off.

*I can upload more (cleaner) pictures if requested, I just didn't want to tear down my other project.*

# Answer
Ok so the problem were the wires. Although I tested every single wire for continuity I went to the store and bought a brand new (expensive/not-from-aliexpress) pack of jumper wires. I rewired the entire project with the schematic and code that was posted in my question-post and it worked like a charm..

So I'm not sure what exactly caused the problem, but it was in the wire. I thought having continuity in a wire is like a 100% proof that the wire works properly. Maybe someone has an idea what could cause this?

So again, I only replaced the wires, not the schematic nor the code, with brand new ones and it worked perfectly.
> 2 votes

# Answer
I have an suggestion. Try removing the comment from this line: `//lcd.setCursor(0,0);`

You need to be sure your text is displaying on the first line, first position(0,0).
> 0 votes

# Answer
The LCD is working perfectly fine (hardware side).

Only 2 changes require in your code.

```
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  pinMode(6,OUTPUT);
  analogWrite(6,90); //sweetspot for backlight without using a potentiometer.

  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print("hello, world!");
  delay(3000);
}

void loop() {
}

```

Added delay (it must!) and set the cursor position.

Try above code and let inform.
> 0 votes

# Answer
(This is a duplicate).

I solved it following this:

> The contrast can be adjusted by the potential-meter displayed in the following picture.

credit
> 0 votes

---
Tags: arduino-uno, lcd, wires
---


# Question
Title: ESP8266-01 wdt reset after uploading code
I'm using code from here: ESP8266 example: Wi-Fi Access point, static IP, web-server and remote GPIO control.

Everything running ok this morning, but now I don't know why wasn't working...

This is what serial monitor display:

```
Configuring access point...
AP IP address: 42.42.42.42
HTTP server started

Exception (0):
epc1=0x40226744 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000000 depc=0x00000000

ctx: sys
sp: 3fffed10 end: 3fffffb0 offset: 01a0

>>>stack>>>
3fffeeb0:  00000000 3fffdad0 3ffeefec 00000030
3fffeec0:  3ffe9560 40226dbb 3fff0104 0000d158
3fffeed0:  00000000 40216ec8 3ffedfe8 4020f68c
3fffeee0:  00f20e64 3ffe9374 3ffe9204 3ffef190
3fffeef0:  4020f7c8 3ffef194 3ffe91fc 60000600
3fffef00:  40226731 3ffe9370 3ffee820 00000001
3fffef10:  40226814 3ffe9374 3ffee820 0331cfa4
3fffef20:  3fff0104 40100ab6 00000001 00000000
3fffef30:  4022df51 40100883 00000000 00000000
3fffef40:  4022df96 3fffdab0 00000000 3fffdcb0
3fffef50:  3ffee838 3fffdad0 3ffeefec 402073e3
3fffef60:  40000f49 40000f49 3fffdab0 40000f49
3fffef70:  40000e19 40001878 00000004 00000000
3fffef80:  3fffff10 aa55aa55 000000ed 40104424
3fffef90:  4010442a 00000004 00000000 a9751f68
3fffefa0:  4010000d e45c7604 ffd63db0 ca695611
3fffefb0:  00000000 3fffef4c 00000000 3fffff08
3fffefc0:  3fffffd0 00000000 00000000 feefeffe
3fffefd0:  feefeffe feefeffe feefeffe feefeffe
3fffefe0:  feefeffe feefeffe feefeffe feefeffe
3fffeff0:  feefeffe feefeffe feefeffe feefeffe
3ffff000:  feefeffe feefeffe feefeffe feefeffe
3ffff010:  feefeffe feefeffe feefeffe feefeffe
3ffff020:  feefeffe feefeffe feefeffe feefeffe
3ffff030:  feefeffe feefeffe feefeffe feefeffe
3ffff040:  feefeffe feefeffe feefeffe feefeffe
3ffff050:  feefeffe feefeffe feefeffe feefeffe
3ffff060:  feefeffe feefeffe feefeffe feefeffe
3ffff070:  feefeffe feefeffe feefeffe feefeffe
3ffff080:  feefeffe feefeffe feefeffe feefeffe
3ffff090:  feefeffe feefeffe feefeffe feefeffe
3ffff0a0:  feefeffe feefeffe feefeffe feefeffe
3ffff0b0:  feefeffe feefeffe feefeffe feefeffe
3ffff0c0:  feefeffe feefeffe feefeffe feefeffe
3ffff0d0:  feefeffe feefeffe feefeffe feefeffe
3ffff0e0:  feefeffe feefeffe feefeffe feefeffe
3ffff0f0:  feefeffe feefeffe feefeffe feefeffe
3ffff100:  feefeffe feefeffe feefeffe feefeffe
3ffff110:  feefeffe feefeffe feefeffe feefeffe
3ffff120:  feefeffe feefeffe feefeffe feefeffe
3ffff130:  feefeffe feefeffe feefeffe feefeffe
3ffff140:  feefeffe feefeffe feefeffe feefeffe
3ffff150:  feefeffe feefeffe feefeffe feefeffe
3ffff160:  feefeffe feefeffe feefeffe feefeffe
3ffff170:  feefeffe feefeffe feefeffe feefeffe
3ffff180:  feefeffe feefeffe feefeffe feefeffe
3ffff190:  feefeffe feefeffe feefeffe feefeffe
3ffff1a0:  feefeffe feefeffe feefeffe feefeffe
3ffff1b0:  feefeffe feefeffe feefeffe feefeffe
3ffff1c0:  feefeffe feefeffe feefeffe feefeffe
3ffff1d0:  feefeffe feefeffe feefeffe feefeffe
3ffff1e0:  feefeffe feefeffe feefeffe feefeffe
3ffff1f0:  feefeffe feefeffe feefeffe feefeffe
3ffff200:  feefeffe feefeffe feefeffe feefeffe
3ffff210:  feefeffe feefeffe feefeffe feefeffe
3ffff220:  feefeffe feefeffe feefeffe feefeffe
3ffff230:  feefeffe feefeffe feefeffe feefeffe
3ffff240:  feefeffe feefeffe feefeffe feefeffe
3ffff250:  feefeffe feefeffe feefeffe feefeffe
3ffff260:  feefeffe feefeffe feefeffe feefeffe
3ffff270:  feefeffe feefeffe feefeffe feefeffe
3ffff280:  feefeffe feefeffe feefeffe feefeffe
3ffff290:  feefeffe feefeffe feefeffe feefeffe
3ffff2a0:  feefeffe feefeffe feefeffe feefeffe
3ffff2b0:  feefeffe feefeffe feefeffe feefeffe
3ffff2c0:  feefeffe feefeffe feefeffe feefeffe
3ffff2d0:  feefeffe feefeffe feefeffe feefeffe
3ffff2e0:  feefeffe feefeffe feefeffe feefeffe
3ffff2f0:  feefeffe feefeffe feefeffe feefeffe
3ffff300:  feefeffe feefeffe feefeffe feefeffe
3ffff310:  feefeffe feefeffe feefeffe feefeffe
3ffff320:  feefeffe feefeffe feefeffe feefeffe
3ffff330:  feefeffe feefeffe feefeffe feefeffe
3ffff340:  feefeffe feefeffe feefeffe feefeffe
3ffff350:  feefeffe feefeffe feefeffe feefeffe
3ffff360:  feefeffe feefeffe feefeffe feefeffe
3ffff370:  feefeffe feefeffe feefeffe feefeffe
3ffff380:  feefeffe feefeffe feefeffe feefeffe
3ffff390:  feefeffe feefeffe feefeffe feefeffe
3ffff3a0:  feefeffe feefeffe feefeffe feefeffe
3ffff3b0:  feefeffe feefeffe feefeffe feefeffe
3ffff3c0:  feefeffe feefeffe feefeffe feefeffe
3ffff3d0:  feefeffe feefeffe feefeffe feefeffe
3ffff3e0:  feefeffe feefeffe feefeffe feefeffe
3ffff3f0:  feefeffe feefeffe feefeffe feefeffe
3ffff400:  feefeffe feefeffe feefeffe feefeffe
3ffff410:  feefeffe feefeffe feefeffe feefeffe
3ffff420:  feefeffe feefeffe feefeffe feefeffe
3ffff430:  feefeffe feefeffe feefeffe feefeffe
3ffff440:  feefeffe feefeffe feefeffe feefeffe
3ffff450:  feefeffe feefeffe feefeffe feefeffe
3ffff460:  feefeffe feefeffe feefeffe feefeffe
3ffff470:  feefeffe feefeffe feefeffe feefeffe
3ffff480:  feefeffe feefeffe feefeffe feefeffe
3ffff490:  feefeffe feefeffe feefeffe feefeffe
3ffff4a0:  feefeffe feefeffe feefeffe feefeffe
3ffff4b0:  feefeffe feefeffe feefeffe feefeffe
3ffff4c0:  feefeffe feefeffe feefeffe feefeffe
3ffff4d0:  feefeffe feefeffe feefeffe feefeffe
3ffff4e0:  feefeffe feefeffe feefeffe feefeffe
3ffff4f0:  feefeffe feefeffe feefeffe feefeffe
3ffff500:  feefeffe feefeffe feefeffe feefeffe
3ffff510:  feefeffe feefeffe feefeffe feefeffe
3ffff520:  feefeffe feefeffe feefeffe feefeffe
3ffff530:  feefeffe feefeffe feefeffe feefeffe
3ffff540:  feefeffe feefeffe feefeffe feefeffe
3ffff550:  feefeffe feefeffe feefeffe feefeffe
3ffff560:  feefeffe feefeffe feefeffe feefeffe
3ffff570:  feefeffe feefeffe feefeffe feefeffe
3ffff580:  feefeffe feefeffe feefeffe feefeffe
3ffff590:  feefeffe feefeffe feefeffe feefeffe
3ffff5a0:  feefeffe feefeffe feefeffe feefeffe
3ffff5b0:  feefeffe feefeffe feefeffe feefeffe
3ffff5c0:  feefeffe feefeffe feefeffe feefeffe
3ffff5d0:  feefeffe feefeffe feefeffe feefeffe
3ffff5e0:  feefeffe feefeffe feefeffe feefeffe
3ffff5f0:  feefeffe feefeffe feefeffe feefeffe
3ffff600:  feefeffe feefeffe feefeffe feefeffe
3ffff610:  feefeffe feefeffe feefeffe feefeffe
3ffff620:  feefeffe feefeffe feefeffe feefeffe
3ffff630:  feefeffe feefeffe feefeffe feefeffe
3ffff640:  feefeffe feefeffe feefeffe feefeffe
3ffff650:  feefeffe feefeffe feefeffe feefeffe
3ffff660:  feefeffe feefeffe feefeffe feefeffe
3ffff670:  feefeffe feefeffe feefeffe feefeffe
3ffff680:  feefeffe feefeffe feefeffe feefeffe
3ffff690:  feefeffe feefeffe feefeffe feefeffe
3ffff6a0:  feefeffe feefeffe feefeffe feefeffe
3ffff6b0:  feefeffe feefeffe feefeffe feefeffe
3ffff6c0:  feefeffe feefeffe feefeffe feefeffe
3ffff6d0:  feefeffe feefeffe feefeffe feefeffe
3ffff6e0:  feefeffe feefeffe feefeffe feefeffe
3ffff6f0:  feefeffe feefeffe feefeffe feefeffe
3ffff700:  feefeffe feefeffe feefeffe feefeffe
3ffff710:  feefeffe feefeffe feefeffe feefeffe
3ffff720:  feefeffe feefeffe feefeffe feefeffe
3ffff730:  feefeffe feefeffe feefeffe feefeffe
3ffff740:  feefeffe feefeffe feefeffe feefeffe
3ffff750:  feefeffe feefeffe feefeffe feefeffe
3ffff760:  feefeffe feefeffe feefeffe feefeffe
3ffff770:  feefeffe feefeffe feefeffe feefeffe
3ffff780:  feefeffe feefeffe feefeffe feefeffe
3ffff790:  feefeffe feefeffe feefeffe feefeffe
3ffff7a0:  feefeffe feefeffe feefeffe feefeffe
3ffff7b0:  feefeffe feefeffe feefeffe feefeffe
3ffff7c0:  feefeffe feefeffe feefeffe feefeffe
3ffff7d0:  feefeffe feefeffe feefeffe feefeffe
3ffff7e0:  feefeffe feefeffe feefeffe feefeffe
3ffff7f0:  feefeffe feefeffe feefeffe feefeffe
3ffff800:  feefeffe feefeffe feefeffe feefeffe
3ffff810:  feefeffe feefeffe feefeffe feefeffe
3ffff820:  feefeffe feefeffe feefeffe feefeffe
3ffff830:  feefeffe feefeffe feefeffe feefeffe
3ffff840:  feefeffe feefeffe feefeffe feefeffe
3ffff850:  feefeffe feefeffe feefeffe feefeffe
3ffff860:  feefeffe feefeffe feefeffe feefeffe
3ffff870:  feefeffe feefeffe feefeffe feefeffe
3ffff880:  feefeffe feefeffe feefeffe feefeffe
3ffff890:  feefeffe feefeffe feefeffe feefeffe
3ffff8a0:  feefeffe feefeffe feefeffe feefeffe
3ffff8b0:  feefeffe feefeffe feefeffe feefeffe
3ffff8c0:  feefeffe feefeffe feefeffe feefeffe
3ffff8d0:  feefeffe feefeffe feefeffe feefeffe
3ffff8e0:  feefeffe feefeffe feefeffe feefeffe
3ffff8f0:  feefeffe feefeffe feefeffe feefeffe
3ffff900:  feefeffe feefeffe feefeffe feefeffe
3ffff910:  feefeffe feefeffe feefeffe feefeffe
3ffff920:  feefeffe feefeffe feefeffe feefeffe
3ffff930:  feefeffe feefeffe feefeffe feefeffe
3ffff940:  feefeffe feefeffe feefeffe feefeffe
3ffff950:  feefeffe feefeffe feefeffe feefeffe
3ffff960:  feefeffe feefeffe feefeffe feefeffe
3ffff970:  feefeffe feefeffe feefeffe feefeffe
3ffff980:  feefeffe feefeffe feefeffe feefeffe
3ffff990:  feefeffe feefeffe feefeffe feefeffe
3ffff9a0:  feefeffe feefeffe feefeffe feefeffe
3ffff9b0:  feefeffe feefeffe feefeffe feefeffe
3ffff9c0:  feefeffe feefeffe feefeffe feefeffe
3ffff9d0:  feefeffe feefeffe feefeffe feefeffe
3ffff9e0:  feefeffe feefeffe feefeffe feefeffe
3ffff9f0:  feefeffe feefeffe feefeffe feefeffe
3ffffa00:  feefeffe feefeffe feefeffe feefeffe
3ffffa10:  feefeffe feefeffe feefeffe feefeffe
3ffffa20:  feefeffe feefeffe feefeffe feefeffe
3ffffa30:  feefeffe feefeffe feefeffe feefeffe
3ffffa40:  feefeffe feefeffe feefeffe feefeffe
3ffffa50:  feefeffe feefeffe feefeffe feefeffe
3ffffa60:  feefeffe feefeffe feefeffe feefeffe
3ffffa70:  feefeffe feefeffe feefeffe feefeffe
3ffffa80:  feefeffe feefeffe feefeffe feefeffe
3ffffa90:  feefeffe feefeffe feefeffe feefeffe
3ffffaa0:  feefeffe feefeffe feefeffe feefeffe
3ffffab0:  feefeffe feefeffe feefeffe feefeffe
3ffffac0:  feefeffe feefeffe feefeffe feefeffe
3ffffad0:  feefeffe feefeffe feefeffe feefeffe
3ffffae0:  feefeffe feefeffe feefeffe feefeffe
3ffffaf0:  feefeffe feefeffe feefeffe feefeffe
3ffffb00:  feefeffe feefeffe feefeffe feefeffe
3ffffb10:  feefeffe feefeffe feefeffe feefeffe
3ffffb20:  feefeffe feefeffe feefeffe feefeffe
3ffffb30:  feefeffe feefeffe feefeffe feefeffe
3ffffb40:  feefeffe feefeffe feefeffe feefeffe
3ffffb50:  feefeffe feefeffe feefeffe feefeffe
3ffffb60:  feefeffe feefeffe feefeffe feefeffe
3ffffb70:  feefeffe feefeffe feefeffe feefeffe
3ffffb80:  feefeffe feefeffe feefeffe feefeffe
3ffffb90:  feefeffe feefeffe feefeffe feefeffe
3ffffba0:  feefeffe feefeffe feefeffe feefeffe
3ffffbb0:  feefeffe feefeffe feefeffe feefeffe
3ffffbc0:  feefeffe feefeffe feefeffe feefeffe
3ffffbd0:  feefeffe feefeffe feefeffe feefeffe
3ffffbe0:  feefeffe feefeffe feefeffe feefeffe
3ffffbf0:  feefeffe feefeffe feefeffe feefeffe
3ffffc00:  feefeffe feefeffe feefeffe feefeffe
3ffffc10:  feefeffe feefeffe feefeffe feefeffe
3ffffc20:  feefeffe feefeffe feefeffe feefeffe
3ffffc30:  feefeffe feefeffe feefeffe feefeffe
3ffffc40:  feefeffe feefeffe feefeffe feefeffe
3ffffc50:  feefeffe feefeffe feefeffe feefeffe
3ffffc60:  feefeffe feefeffe feefeffe feefeffe
3ffffc70:  feefeffe feefeffe feefeffe feefeffe
3ffffc80:  feefeffe feefeffe feefeffe feefeffe
3ffffc90:  feefeffe feefeffe feefeffe feefeffe
3ffffca0:  feefeffe feefeffe feefeffe feefeffe
3ffffcb0:  feefeffe feefeffe feefeffe feefeffe
3ffffcc0:  feefeffe feefeffe feefeffe feefeffe
3ffffcd0:  feefeffe feefeffe 40227cb3 00000001
3ffffce0:  ffffffff 00000000 3ffe96b1 00000008
3ffffcf0:  40227d02 3ffeccf8 3ffefadc 00000001
3ffffd00:  3fff07e8 00000000 4010106b 3ffeccf8
3ffffd10:  000000c0 4021a0c2 00000008 3fff06d8
3ffffd20:  40226d70 3ffeccf8 3ffed470 3fff07e8
3ffffd30:  00000000 4021a22b 3ffefd84 feefeffe
3ffffd40:  00000000 00000002 00000001 3ffeccf8
3ffffd50:  00000000 400042db 3fff07e8 3ffeff44
3ffffd60:  40004b31 3fff080c 0000049c 0007e000
3ffffd70:  401057e0 4020dfcd 3ffef154 3ffeff44
3ffffd80:  40106475 3fff080c 0000049c 00000000
3ffffd90:  00000000 00000000 00000020 40100f22
3ffffda0:  00000000 00000000 00000020 40100f22
3ffffdb0:  3ffe9560 40104304 3fff0104 40100f22
3ffffdc0:  00000001 40103523 3ffed6e0 40100f22
3ffffdd0:  40103986 00000000 00000020 40100f22
3ffffde0:  4010346a 0000007f 7fffffff 00000000
3ffffdf0:  4010346a 0000007f 7fffffff 00000000
3ffffe00:  0000007f 00040000 7fffffff 00000000
3ffffe10:  40103269 00040000 7fffffff 00000000
3ffffe20:  00000000 00000000 0000001f 40104bfd
3ffffe30:  00000000 00000000 0000001f 40104bfd
3ffffe40:  4000050c 00000030 00000011 ffffffff
3ffffe50:  00000000 00000000 0000001f 40104bfd
3ffffe60:  4000050c 00000002 03316038 00000000
3ffffe70:  4020386b 00000030 00000010 ffffffff
3ffffe80:  00000000 00000000 0000001f 40104bfd
3ffffe90:  4000050c 00000000 00000000 fffffffe
3ffffea0:  40000ea6 00000030 0000001e ffffffff
3ffffeb0:  40000ea3 00000000 00000001 00000001
3ffffec0:  3fffdab0 00000000 3fffd9d0 3ffeefec
3ffffed0:  00000000 00000000 00000001 3ffeefe4
3ffffee0:  00000000 3fffdad0 3ffeefec 00000030
3ffffef0:  ffffffff 3fffc6fc 00000001 3ffe85e8
3fffff00:  00000000 3fffdad0 3ffeefec 00000030
3fffff10:  00000000 3fffdad0 3ffeefec 00000030
3fffff20:  402077a4 3ffeedfc 000003e8 402074e2
3fffff30:  3ffe8f00 00000000 00000000 40207525
3fffff40:  3ffe8cac 3ffeedfc 3ffeee00 40203fbe
3fffff50:  3fff014c 00ffffff 3ffeef2c 4020671c
3fffff60:  3fffdad0 3ffeedfc 3ffeedfc 40207918
3fffff70:  4020290c 00000000 3ffeedfc 40204f31
3fffff80:  40207b28 00000000 00001388 40207c40
3fffff90:  00000000 00000000 feefeffe feefeffe
3fffffa0:  00000000 00000000 00000001 40207449
<<<stack<<<

 ets Jan  8 2013,rst cause:4, boot mode:(1,7)

wdt reset

 ets Jan  8 2013,rst cause:4, boot mode:(1,7)

wdt reset

```

Could someone please tell me how can I make it work again?

# Answer
I tried the sketch you posted and it works.

The most likely problem is the power source for the ESP8266. You have in the title ESP8266-1 so I will assume you have a ESP-01 module.

A lot of people who start with the ESP-01, or any other module without an onboard 3.3v regulator, don't know how demanding the ESP8266 is on the power source. It needs to have a stable 500mA capacity for it to work reliably. Also the regulator must be electrically close to the module. Using long connecting wires is one problem I see often.

The ESP8266 could have enough power to get programmed correctly but when it comes to transmitting it will draw a lot more current and if the power supply is insufficient a voltage dip occurs and then unpredictable behaviour occurs.

Check over any connections that might be poor. Please post what you are using for hardware if you can't get things running again.
> 2 votes

---
Tags: esp8266, web-server
---


title: Is there something wrong in my sketch?

text:
# Question
Title: Is there something wrong in my sketch?
I've been working on a project that need two sensors(mq2 and mpu6050), I got each sketches from the internet and I managed to work the mq2 but for the mpu6050, I'm still not sure if its working. The thing is I want the two sensors to work on their own but in one sketch. When the mq2 senses some gas, I want the LED and BUZZER turned on and the only way to turn it off is by resetting the arduino. For the MPU6050, it's the same with the mq2, when the MP6050 is triggered, I want the other LED and the same buzzer turn on, again the only way to turn it off is by resetting the arduino. I want to combine the se two skectches in one. But when I combined it, the circuit won't work. Could someone help me ?

Here is the code that I modified and combined:

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

#define minval -5
#define maxval 3

MPU6050 mpu;
//////////////////////////////////////////////////////////////////

//for mq2

int redLed = 12;
int greenLed = 11;
int buzzer = 10;
int smokeA0 = A5;
// Your threshold value
int sensorThres = 100;

//////////////////////////////////////////////////////////////////

void setup(){
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(smokeA0, INPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  mpu.setThreshold(3);
}

//////////////////////////////////////////////////////////////////

void loop(){
  // for mq2

  int analogSensor = analogRead(smokeA0);

  // Checks if it has reached the threshold value
  if (analogSensor > sensorThres)
  {
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
    tone(buzzer, 1000);
  }
  else{
  digitalWrite(greenLed, HIGH);
  }
  // for MPU6050

  Vector rawGyro = mpu.readRawGyro();
  Vector normGyro = mpu.readNormalizeGyro();
  if (normGyro.XAxis > maxval ||
      normGyro.XAxis < minval &&
      normGyro.YAxis > maxval ||
      normGyro.YAxis  < minval &&
      normGyro.ZAxis > maxval ||
      normGyro.ZAxis  < minval)
  {
    digitalWrite(7,HIGH);
    digitalWrite(8,HIGH);
  }
  else
  {
    digitalWrite(7,LOW);
    digitalWrite(8,LOW);
  }

}

```

Here is the code for the mq2:

```
int redLed = 12;
int greenLed = 11;
int buzzer = 10;
int smokeA0 = A5;
// Your threshold value
int sensorThres = 150;

void setup() {
  pinMode(redLed, OUTPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(smokeA0, INPUT);
}

void loop() {
  int analogSensor = analogRead(smokeA0);

  // Checks if it has reached the threshold value
  if (analogSensor > sensorThres)
  {
    digitalWrite(redLed, HIGH);
    digitalWrite(greenLed, LOW);
    tone(buzzer, 1000);
  }
  else{
  digitalWrite(greenLed, HIGH);
  delay(100);
  }
}

```

Here is the code for the MPU6050(I removed some lines when I put it at the combined sketch because I dont need the LCD code, dunno if I did it correct):

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

#define minval -5
#define maxval 3

MPU6050 mpu;

void setup()
{
  Serial.begin(115200);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);

  delay (2000);

  // Initialize MPU6050
  Serial.println("Initialize MPU6050");

  while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))
  {
    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
    delay(500);
  }

  mpu.setThreshold(3);
  // Check settings
  checkSettings();
}

void checkSettings()
{
  Serial.println();
  Serial.print(" * Sleep Mode:        ");
  Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled");
  Serial.print(" * Clock Source:      ");
  switch(mpu.getClockSource())
  {
    case MPU6050_CLOCK_KEEP_RESET:
      Serial.println("Stops the clock and keeps the timing generator in reset");
      break;
    case MPU6050_CLOCK_EXTERNAL_19MHZ:
      Serial.println("PLL with external 19.2MHz reference");
      break;
    case MPU6050_CLOCK_EXTERNAL_32KHZ:
      Serial.println("PLL with external 32.768kHz reference");
      break;
    case MPU6050_CLOCK_PLL_ZGYRO:
      Serial.println("PLL with Z axis gyroscope reference");
      break;
    case MPU6050_CLOCK_PLL_YGYRO:
      Serial.println("PLL with Y axis gyroscope reference");
      break;
    case MPU6050_CLOCK_PLL_XGYRO:
      Serial.println("PLL with X axis gyroscope reference");
      break;
    case MPU6050_CLOCK_INTERNAL_8MHZ:
      Serial.println("Internal 8MHz oscillator");
      break;
  }

  Serial.print(" * Gyroscope:         ");
  switch(mpu.getScale())
  {
    case MPU6050_SCALE_2000DPS:
      Serial.println("2000 dps");
      break;
    case MPU6050_SCALE_1000DPS:
      Serial.println("1000 dps");
      break;
    case MPU6050_SCALE_500DPS:
       Serial.println("500 dps");
       break;
    case MPU6050_SCALE_250DPS:
       Serial.println("250 dps");
       break;
  }

  Serial.print(" * Gyroscope offsets: ");
  Serial.print(mpu.getGyroOffsetX());
  Serial.print(" / ");
  Serial.print(mpu.getGyroOffsetY());
  Serial.print(" / ");
  Serial.println(mpu.getGyroOffsetZ());
  Serial.println();
}

void loop()
{
  Vector rawGyro = mpu.readRawGyro();
  Vector normGyro = mpu.readNormalizeGyro();
  Serial.print(" Xraw = ");
  Serial.print(rawGyro.XAxis);
  Serial.print(" Yraw = ");
  Serial.print(rawGyro.YAxis);
  Serial.print(" Zraw = ");
  Serial.println(rawGyro.ZAxis);

  if (normGyro.XAxis > maxval ||
      normGyro.XAxis < minval &&
      normGyro.YAxis > maxval ||
      normGyro.YAxis  < minval &&
      normGyro.ZAxis > maxval ||
      normGyro.ZAxis  < minval)
  {
    digitalWrite(7,HIGH);
    digitalWrite(8,HIGH);
    delay(300);
    digitalWrite(7,HIGH);
    digitalWrite(8,HIGH);
    delay(300);
    delay (1000);
  }
  else
  {
    digitalWrite(7,LOW);
    digitalWrite(8,LOW);
  }
  Serial.print(" Xnorm = ");
  Serial.print(normGyro.XAxis);
  Serial.print(" Ynorm = ");
  Serial.print(normGyro.YAxis);
  Serial.print(" Znorm = ");
  Serial.println(normGyro.ZAxis);
  delay(10);
}

```

# Answer
The Arduino Uno has the special functions on fixed pins.
The I2C bus, for example, uses pin A4 and A5.

In the Arduino Wire reference page is a table of the pin numbers for the Arduino boards.

The pins called "SDA" and "SCL" near the USB connector are connected to the pins "A4" and "A5" (for an Arduino Uno).
That means that if you use the I2C bus, you may not use pin "A4" and "A5" for something else.

Connect the MQ2 gas sensor to pin A0, A1, A2 or A3, and it should work.

The MPU-6050 is an old sensor, there are newer and better sensors with less noise.
The MPU-6050 is a 3.3V sensor, that means its power should be 3.3V and the I2C bus should have 3.3V levels.
The Arduino Uno has a microcontroller that runs at 5V, and therefor it has a I2C bus with 5V levels.
A I2C level shifter can be used to connect the Arduino Uno with the MPU-6050. You can also use 4k7 pullup resistors from SDA to 3.3V and from SCL to 3.3V. Those pullup resistors are often already on the MPU-6050 module.

The MPU-6050 will work (for some time) with a I2C bus that has 5V levels, it might even work when the MPU-6050 is powered with 5V directly to the chip. That will influence the data, and the sensor might get damaged. Newer 3.3V sensor chips are often faster damaged with 5V, but don't push your luck with the MPU-6050.
> 0 votes

---
Tags: arduino-uno, mpu6050
---


# Question
Title: How to make Built in LED blink systematically?
I am trying to make my built in LED on my arduino blink every 1 second while recording my data and putting it in the EEPROM, but I am not sure how to do that. I am supposed to print data to the EEPROM every minute, until the EEPROM is full and while that is going on my built in LED should blink for approx 1 second. My problem seems to be that I am setting a delay to delay how long the data is stored on the EEPROM, but it is also affecting the time it takes for my LED to blink as it is waiting to blink as the data is stored. Any help would be appreciated, the code is below:

```
#include<EEPROM.h>
const int SWITCH = 4;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(SWITCH, INPUT_PULLUP);

}

void loop() {
  // put your main code here, to run repeatedly:
  int addr = 0;
  int r = analogRead(0);

  if (digitalRead(SWITCH) == LOW) {
    Serial.println("----Recording----");
  }
  while (addr <= 1024) {
    digitalWrite(LED_BUILTIN, HIGH);
    delay (250);
    digitalWrite(LED_BUILTIN, LOW);
    delay(750);
    Serial.println(r);
    EEPROM.put(addr, r);
    addr = addr + 2;
    delay(600);
  }
  while (true);
}

```

# Answer
Do not use delay(). Use the example Blink without delay example as the starting point.

https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

Find it under Examples 02.Digital

Simple code like delay() is called blocking code. It prevents other things from happening until t is through. That is acceptable in very sequential linear programs where only one thing is ever happening. And you have discovered the limitation of that. Time to get more sophisticated in your programming.

Please open the example and play with it. Make sure you understand it before thinking of how to modify your code.
> 3 votes

# Answer
As Rudy mentions in his answer, don't use `delay()`, and start with the `BlinkWithoutDelay` example that comes with the Arduino IDE. Here is a sketch to get you started. If you compare it to the `BlinkWithoutDelay` example, it's very similar, except I added some button code to "start" and "stop" the LED flashing.

```
const byte SWITCH = 4;
const byte ledPin = 13;
const unsigned long oneSecond = 1000;
unsigned long previousMillis = 0;
byte ledState = LOW;
byte recording = 0;

void setup(){
  pinMode(ledPin, OUTPUT);
  pinMode(SWITCH, INPUT_PULLUP);
}

void loop(){

  unsigned long currentMillis = millis();

  if(digitalRead(SWITCH) == LOW){
    recording = 1;
  }
  else{
    recording = 0;
    digitalWrite(ledPin, LOW);
  }

  if(currentMillis - previousMillis >= oneSecond){

    previousMillis = currentMillis;

    if(recording){
      if(ledState == LOW){
        ledState = HIGH;
      }
      else{
        ledState = LOW;
      }
    }
    digitalWrite(ledPin, ledState);
  }
}

```
> 1 votes

---
Tags: arduino-uno
---


# Question
Title: Powering a 5V Arduino Pro Mini with 3.3V
I want to run the 5V board at 3.3V because I can get the 5V version cheaper than the 3.3V version.

I read that you can power the Arduino Pro Mini with a battery by connecting the battery to the 5V pin, bypassing the regulator.

Somewhere else I read that the only difference between a 3.3V and 5V Pro Mini is the regulator.

Are these assumptions correct? If yes, can I use a 5V Pro Mini like a 3.3V by powering it with a 3V Battery on the 5V pin?

# Answer
I just discovered that I knew the answer to this, because I was accidentally running some 5v/16MHz Pro Mini boards at 3.3v without issue.

**Short Version**

Yes, you can run one feeding it 3.3v, but you need to program it as a 5v/16MHz board to get the clock speeds right. Also, all your TTL levels (outputs) will be 3.3v, not 5v. Additionally, at least in my experience, the processor will run at 16MHz and not 12MHz with 3.3v.

**Background Story**

I had some generic Pro Mini boards that didn't mark their voltage at all, so I tried programming (and running) them using my FTDI at 3.3v using the 3.3v/8mHz option in the Arduino IDE. I was able to upload programs just fine, but the clock was clearly wrong, as the "Blink" program ran about 2x as fast. That told me that it was running faster than 8MHz .

So I tried again this time telling the IDE it was a 5v/16MHz chip, and everything ran fine, despite the fact that I was feeding it 3.3v. I even saw that pin 13 was cycling from 0v up to 3.3v - exactly what I was feeding it. So for a while I just thought they were 3.3v Pro Minis.

But then thinking about it, I realized that having to tell it to run at 16MHz to get the clock right meant it really was a 16MHz chip - which I didn't think was available with the 3.3v model.

So on a whim (and since I have a ton of these little boards), I changed my FTDI to 5V, and was able to run and program the board just fine.

So from that, I conclude that yes, you can run them at 3.3v, but your TTL levels will be just 3.3v.
> 2 votes

# Answer
Pro Mini 5V has voltage regulator for 5V and 16MHz quartz. Pro Mini 3.3V has voltage regulator for 3.3V and 12MHz quartz.

According to manufacturer specification Atmega328 chip powered by 3.3V cannot run 16MHz and needs at most about 13MHz. But it's official specification. Some chips are better than specification (faster) some are worse (slower). So you can try whether **your** Pro Mini is better or not. Or you can change quartz to 12MHz, if you can solder SMD parts.

Or better look for cheaper 3.3V clone, they are not much more expensive as 5V version...
> 2 votes

# Answer
First I am going to say **it is very much possible.** Allow me to explain.

The 5v version can be powered with 3.3v, but it is **not recommended** to supply power through the 5v pin, bypassing the regulator. **Only *regulated* 5v power should be supplied through the 5v pin. Any other voltage levels should go in the RAW pin.** If you supply the Arduino Pro Mini 5b with 3.3v the chip will only be underclocked to 8-12MHz, and the voltage for Vcc (the board's operating voltage and what it will power its pins with) will be 3.3v; but there is another way.

I primarily source my components from SparkFun due to their reliability, cheap price and awesome support. They provide the Arduino Pro Mini in 3.3v and 5v variants for only $10. I have used both variants for many purposes, but primarily when working with embedded and small solutions. One of those times I was accessing sensors that ran on 5v TTL logic, but I didn't have room to place a \>5v battery, only room for a LiPo (which generally powers 3.3v).

I was able to solve this issue with a SparkFun 5v Step-Up Breakout\*, which boosts the voltage to 5v without any other major issues. I should warn you that there are some issues you could face if you are using a LiPo to feed into the Step-Up. Even if you aren't I'll list it here for reference. The Step-Up's maximum safe input voltage is 4v, and can be a bit unreliable if the voltage goes above it. Unfortunately a LiPo's discharge curve places it above 4v when it is within the upper 95% soc (state-of-charge), so a 3.3v regulator (which SparkFun also sells) would be needed to reliably power an Arduino Pro Mini 5v through a Step-Up breakout (at least in my experience). Also, power from a Step-Up breakout is regulated.

\*Pololu also sells a comparable Step-Up that could handle an input up to 5.5v in theory, but I haven't personally used it.
> 1 votes

# Answer
Im feeding 5V pro mini clone with 2 AA battery, to VCC pin, about 3V and it works normally. It consumes few current compared to 5V.   And you can see what it the setup in here. Also this video can be useful.
> 1 votes

# Answer
> If yes, can I use a 5V Pro Mini like a 3.3V by powering it with a 3V Battery on the 5V pin?

two options (at least):

1) apply 3.3v at the regulator *output* pin. there may be already a break-out for that on the board;

2) solder in a 3.3v regulator in place of the 5v regulator -\> fairly easy to do as well.
> 0 votes

---
Tags: arduino-pro-mini, voltage-level, battery
---


title: Stacks on arrays on arrays

text:
# Question
Title: Stacks on arrays on arrays
I am trying to find a "vanilla" way of writing several arrays into one. I spent a lot of time searching for how to do this but everything that comes up is either a tutorial that doesn't cover concatenation or someones question that ended up being an unrelated syntax problem.

I put several strands of WS2812B LEDs wired in series so the Arduino sees them as one continuous strand. I have been able to run some very cool patterns as one strand using the Pololu library but I would like to program the strands separately. The number of lights I have is very resource intensive and I like to keep things simple so I'm looking for advice on the best way to go about combining arrays without importing more libraries.

Because the patterns I have used so far write to an array and the output is an array, my first thought was that I could write one pattern to an array made up of sections 1, 4, and 5, and another pattern that would be mapped to sections 2, 3, and 6 and then concatenate it all together in order. However, adding arrays seems to be somewhat complicated without worrying about this or that persons favorite library or looping through one value at a time and I am trying to work on my C skills. Is there a simple way to concatenate arrays or a better way for me to think about this? Any best practice or organizational advice is welcome. I want to write clean code.

Bonus Round: I use several different languages at work and I cant figure out why I have it in my head that this would work...

```
array1 = [a, b, c]
array2 = [x, y]
array3 = array1 + array2

// array3 = [a, b, c, x, y]

```

Any idea if that is possible or what language I am thinking of?

# Answer
> 3 votes
C(++) does not natively let you "add" arrays like that. Your code looks like maybe Python.

There are several ways to do what you want to do. You could make your code which writes the pattern to the array accept an offset - so rather than always starting at position 0, you could tell it where to start.

You could write a function to "concatenate" arrays, or use one someone else has written, if you can find one. It's pretty trivial to do.

However the method I'd use in your situation, probably just a matter of preference really, is this:

Let's say (for example) you have 6 strips of 10 LEDs each. You need 60 elements to represent them all. (I'm assuming each element is an `int`; I can't remember if it is.) Create one big array for the whole lot:

```
int allElements[60];

```

You might think of the variable `allElements` as an array, but it is really an `int*` (a pointer to an `int`), with that memory address happening to have 59 other memory addresses after it reserved. C knows that when you say `allElements[7]` it needs to looks 7 `int`s on from the address pointed to by `allElements`. What this means is that you can create new variables which point to parts of that array, and use those variables as if they were their own arrays. For example:

```
int *firstStrip = &(allElements[0]);
int *secondStrip = &(allElements[10]);
int *thirdStrip = &(allElements[20]);
/* ... and so on */

```

(There is a more idiomatic way to write this which is exactly equivalent, but I think the above is more explicit if you're new to it.)

As an extra improvement you might now notice that you have a load of similarly-named variables with similar functions -- and you might want to put them into an array.

```
int* strips[] = { &(allElements[0]), &(allElements[1]), &(allElements[2]) /* and so on */};

```

Now you can use `strips[3]` to give you the fourth strip.

There are several other ways too, but I think this will get you going.

---
Tags: array, led-strip
---


# Question
Title: Does NodeMCU have some protection against wrong connections that could kill a computer USB?
I know *some* computer USB could handle some bad connection (by blocking the port, which need to reboot the computer). But I wonder if a NodeMCU 8266 offers some protection.

# Answer
Most USB ports are shut down when an over-current event is detected.

There is nothing in a NodeMCU that can prevent an over-current event in the case of things like short circuits. The 5V (or VIN on some versions) pin is directly (or sometimes via a single diode) connected to the 5V pin of the USB. Cause a short on the 5V pin and you cause a short on the USB.

Protection is designed to be at the port level not the device level so that devices don't have to be forced to comply to protection "standards" that would drive up costs and result in devices being non-compliant and risk damaging the computer.

If you find that you are often causing shorts on the 5V pin and killing (albeit temporarily) your USB ports you should change your working practices.

It can be good to always connect your devices to an external powered USB hub instead of directly to your computer. That way if you kill a port you can just unplug the hub and plug it back in again to reset all the ports. No need to reboot your computer.

Cheap hubs often just have a simple PTC fuse instead of proper over-current protection which can actually be more useful for device developers since the port won't be disabled, just the external power shut off.

Most Arduino boards do incorporate a PTC fuse in the USB power circuitry, but NodeMCU and similar boards don't.
> 1 votes

---
Tags: usb, nodemcu, voltage-protection
---


# Question
Title: LB1847 stepper controller :how to connect to grbl atmega based board with step ,direction,enable , limit?
I like to use an LB1847 chip with stepper motor "both salvaged from old copier". I made a GRBL AtMega board, this board gives step, direction, enable and limit leads.

So I tried to find the corresponding pins on the LB1847, but the data sheet only shows enable1 and enable2 pins and phase1 and phase2 pins?

So how can I connect the step and direction and enable pins of AtMega to these pins of LB1847?

There are other logic pins on LB1847 chip to control current and decay. If the data sheet is required i can upload it.

Thanks in advance

# Answer
It seems that most people use an Arduino CNC Shield to connect their Arduino Uno (running GRBL software) to the drivers of the stepper-motors. Via Google you find plenty of them, for example: here and here. The schematics of the latter example are published here. You could use the information from this schematic to make your own 'interface' between the Arduino and the LB1847 driver chips.
> 0 votes

---
Tags: stepper
---


# Question
Title: ESP8266 library: 2 requests received instead of 1
I have a strange issue with my sketch using WiFiEsp.h library from https://github.com/bportaluri/WiFiEsp

When i send 1 request to my ESP8266 from my browser as http://192.168.0.25, it seems that my ESP8266 receives 2 requests and run 2 times. See the sketch here and Serial Termninal:

```
void loop()
{
  // listen for incoming clients
  WiFiEspClient client = server.available();
  if (client) {
    Serial.println("New client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          Serial.println("Sending response");

          // send a standard http response header
          // use \r\n instead of many println statements to speedup data send
          client.print(
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "Connection: close\r\n"  // the connection will be closed after completion of the response
            "\r\n");
          client.print("<!DOCTYPE HTML>\r\n");
          client.print("<html>\r\n");
          client.print("Hello World!\r\n");
          client.print("</html>\r\n");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(10);

    // close the connection:
    client.stop();
    Serial.println("Client disconnected");
  }
}

```

Serial temrinal:

```
[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.0.0
Attempting to connect to WPA SSID: Numericable-6010
[WiFiEsp] Connected to Numericable-6010
You're connected to the network
SSID: Numericable-6010
IP Address: 192.168.0.25

To see this page in action, open a browser to http://192.168.0.25

[WiFiEsp] Server started on port 80
[WiFiEsp] New client 0
New client
GET / HTTP/1.1
Host: 192.168.0.25
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://192.168.0.25/
Accept-Encoding: gzip, deflate
Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7

Sending response
[WiFiEsp] Disconnecting  0
Client disconnected
[WiFiEsp] New client 1
New client
GET /favicon.ico HTTP/1.1
Host: 192.168.0.25
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://192.168.0.25/
Accept-Encoding: gzip, deflate
Accept-Language: fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7

Sending response
[WiFiEsp] Disconnecting  1
Client disconnected

```

Please, could you explain why 2 clients connected ? How can i disconnect just after the first one ?

# Answer
Browsers automaticaly request /favicon.ico for sites. Add this to `<head>` section of your HTML to eliminate the 'favicon' request:

`<link rel="icon" href="data:;base64,iVBORw0KGgo=">`

```
client.println("HTTP/1.1 200 OK\r\n" "Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html><head>");
client.println("<link rel='icon' href='data:;base64,iVBORw0KGgo='>");
client.println("</head><body>");
client.println("Hello World!");
client.println("</body></html>");

```

based on this SO answer, instead of pointing to a server, the icon is encoded in the href parameter as base64 string
> 1 votes

# Answer
Adding head part with hortcut icon link in html response fix the issue. Thanks !!!

```
            client.print(
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "Connection: close\r\n"  // the connection will be closed after completion of the response
            "\r\n");
          client.print("<!DOCTYPE HTML>\r\n");
          client.print("<head>\r\n");
          client.print("<link rel='shortcut icon' href='data:image/x-icon;,' type='image/x-icon'> \r\n");
          client.print("</head>\r\n");
          client.print("<html>\r\n");
          client.print("Hello World!\r\n");
          client.print("</html>\r\n");

```
> 0 votes

---
Tags: arduino-mega, esp8266, library
---


title: How do I use a servo in an interrupt?

text:
# Question
Title: How do I use a servo in an interrupt?
I am working on a small circuit now where I increment the rotation of a servo motor and at the same time blink a LED.

I have used all methods I know to do it but still it does not work or it blinks and then the servo start working but never both at the same time. This is what i am basically working with:

```
  for(position=0;position<=90;position=position+5){
    digitalWrite(led_pin,HIGH);
    delay(200);
    digitalWrite(led_pin,LOW);
    delay(200);
    servoBar.write(position);
    delay(15);
  }

```

But it blinks once and then it stops and the servo start working right after that.

I know I must not use delays in interrupts. I am using a function that I can but just to show you what I am working with.

# Answer
> 3 votes
The problem is that the delay routine is blocking. It stops operation and blocks other things from happening. Have a look at the Blink without delay example in the example tab. (also linked below) https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

What you need to do is to not block. Call the time checker often and check if the time you want delayed has run out. The perform the action needed to be done at that time.

This is a more sophisticated way to deal with timed events. Also look at this **Demonstration code for several things at the same time.** https://forum.arduino.cc/index.php?topic=223286.0

Have fun learning. :)

# Answer
> 0 votes
A good way to manage timing without delay is to use:

```
if(millis()-lastCheck > delayPeriod){
lastCheck=millis();
doStuffHereOrChangeServo();
}

```

This code is naturally overflow-safe and is actually how delay() works. **The difference is that delay() uses a while() loop that breaks when this condition is met, so it prevents any code after the delay from running UNTIL delay completes. That's why you are getting odd pauses in your code.**

As a side note, it's actually a fun project to build a simple task scheduler that uses a list of function pointers and a variation on this method to do completely generic scheduling. If you want to try that route, you'd keep a struct holding the function pointer and a *signed* integer larger than your maximum intended delay size. Then, every time the scheduler is called, you subtract the difference between millis() and the stored value of millis() from each of those integers in the struct. Once that goes negative or zero, the task can run!

---
Tags: arduino-uno
---


# Question
Title: Getting individual outputs from 4 load cells
This is my first time using an Arduino, and I want to connect 4 load cells to one Arduino board and then get 4 separate outputs for each load cell on a computer. What would I need to accomplish this and how would I set it up? Can all four load cells connect to a single Arduino?

Also, could I get it to work with these load cells in particular:

https://www.amazon.co.uk/HALJIA-Weighing-Resistance-Half-bridge-Original-Silver/dp/B0792WCGJC/

or would I have to use ones with a 4 wire output?

# Answer
> 2 votes
For this purpose there are 'simple' interface boards like the HX711 (google on HX711). A guy named Bogde has written an Arduino library to manage this board which you can find on GitHub (https://github.com/bogde/HX711). There you can also subscribe to the corresponding email group, which is rather lively and also handles similar questions as yours.

Also Sparkfun sells these sort of interface boards and has very good and extensive documentation and tutorials on the site (see comment from VE7JRO below your question).

You can manage 4 load cells with one Arduino depending on the frequency with which you want to read them. If all 4 load cells are used to weigh one platform then Sparkfun sells an interface board to combine them in one "weight sensor" making it even more simple to interface with the Arduino.

---
Tags: arduino-uno
---


# Question
Title: Digispark don't work when I power it by 5v and GND inputs and chip starts heating
I'm beginner so probably I did something stupid, but now my digispark don't work when I power it by 5v and GND inputs but chip starts heating immediately. When I power it by USB everything works well. Did i broke it? And how can I check it?

# Answer
You connected the power backwards - so all the current is being shunted straight through the ESD protection diodes (and likely other routes too) generating lots of heat and risking permanent damage to the MCU.

Take better care next time...
> 1 votes

---
Tags: digispark
---


# Question
Title: Feed values to an array using keypad interfaced with Arduino Uno
I am trying to input keypad values to an array. The code compiles without any error. When running the code, the code proceeds into the for loop and prints 'm' on the lcd, but doesn't execute "customKey = customKeypad.getKey();" and the below steps and keeps printing 'm' 5 times. I am not able to enter keypad values. Please help.

```
#include <LiquidCrystal.h>
#include <Keypad.h>

const int rs = A1, en = A0, d4 = A2, d5 = A3, d6 = A4, d7 = A5;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

const byte ROWS = 4;
const byte COLS = 4;

char hexaKeys[ROWS][COLS] = { { '1', '2', '3', 'A' }, { '4', '5', '6', 'B' }, {
    '7', '8', '9', 'C' }, { '*', '0', '#', 'D' } };

byte rowPins[ROWS] = { 13, 12, 11, 10 };
byte colPins[COLS] = { 9, 8, 7, 6 };

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS,
    COLS);

//String inputString = "";
char customKey;
char a[5];

void setup() {
  Serial.begin(9600);
  //inputString.reserve(200);
  lcd.begin(16, 2);
}

void loop() {
  lcd.clear();
  if (Serial.read() == '1') {
    lcd.setCursor(0, 0);
    lcd.print("WELCOME !");
    delay(1000);
    lcd.clear();
    lcd.print("Enter UserID :");
    lcd.setCursor(0, 1);
    for (int i = 0; i < 5; i++) {
      lcd.print('m');
      customKey = customKeypad.getKey();
      delay(30);

      if (customKey) {
        lcd.print('*');
        a[i] = customKey;
      }
      if (i == 5) {
        a[i] = '\0';
        break;
      }

      delay(1000);
    }
  }
}

```

# Answer
> 2 votes
`customKeypad.getKey();` doesn't wait for the key press. it returns 0 if no key is pressed.

you should rewrite the sketch to a version where the loop() can loop and key presses are read as they are pressed.

---
Tags: arduino-uno
---


# Question
Title: Getting incorrect output from ADC over SPI in Arduino Uno
I am trying to read data from an ADC ADCS7476 (datasheet) using SPI on Arduino Uno. I am using the following code to read the data from the ADC and printing it on serial monitor. However, the code is always printing 0. I never used SPI before and I am not able to debug the issue, does anyone know where I am going wrong.

I am using the following connections:

1. ADC's SCLK to Pin 13 on Ardunio
2. ADC's SDATA to Pin 12 on Arduino
3. ADC's CS to Pin 10 on Arduino

Code:

```
#include <SPI.h>
#define cs 10

SPISettings settingsA(250000, MSBFIRST, SPI_MODE0);
byte highbyte,lowbyte;
unsigned int data;

void setup() {
  Serial.begin(9600);
  pinMode(cs, OUTPUT);
  digitalWrite(cs, HIGH);
  delay(1000);
  SPI.begin();
}

void loop() {
  SPI.beginTransaction(settingsA);
  digitalWrite(cs, LOW);
  delayMicroseconds(1);
  highbyte = SPI.transfer(0xFF);
  lowbyte = SPI.transfer(0xFF);
  digitalWrite(cs, HIGH);
  delayMicroseconds(1);
  SPI.endTransaction();
  data= ((unsigned int)highbyte << 8 ) + lowbyte;
  Serial.println(data);
  delay(100);
}

```

# Answer
> 1 votes
*I am leaving this answer in case anyone stumbles across this question in the future*

In a super frustrating, anti-climactic end, it turns out that the ADC chip itself was not working properly. I tested the (faulty) chip using the code above and observed the output of data pin on the oscilloscope and the output was the same square wave irrespective of the input. I managed to find a new ADC and I tested it and it was working correctly. But this raises a new issue for me, this is the third ADC chip I tested that was working correctly, the first two were faulty. It is very unlikely that it is a manufacturing issue and I used the same connections and the same code to test all three of them. Maybe it has to something to do with soldering, because AFAIK that's the only thing that's different. If anyone knows anything else that might have gone wrong, I'm all ears.

---
Tags: arduino-uno, spi, adc
---


title: Arduino Power Consumption

text:
# Question
Title: Arduino Power Consumption
I am pairing an Arduino Uno along with both a HC-05 bluetooth module and a sensor circuit.

Show below is the circuit conditions as well as the circuit.

As far as powering up the system, I am planning to use a 9 Volt battery(550 ampere) to connect to the Arduino which draws in 45 mA and the sensor circuit which takes in a total of 28 mA. I calculated the total life of one of the batteries to be 7.53 hours by (550/(28+45)). Can someone help me with the total power consumed by the system?

UPDATE: I read on the sensor data sheet that there are two parameters which heater power as well as sensor consumption. Heater Power consumers approximately 140 mW and the sensor consumes approximately 10 mW. I believe the Arduino will consume 405 mW because of (9 volts * 45 mA) and the bluetooth module will consume 26.5 mW because (3.3 volts * 8). Therefore, the total power consumption is 571.5 mW. Is this correct??? Also will the 9 volt battery be able to support the total power?

# Answer
Your calculations are not totally right. I did not check the data you provided, so I'm just using the numbers you put.

The arduino draws up 45mA @5V. The sensor has a power of 150 mW and is powered at 5V, which means a current of 30mA. The bluetooth module gets 8mA (are you really sure? it seems too low) @3.3V.

Now, the battery gives you 9V. So you need to convert them to the proper (5V and 3.3V) voltages required by the devices.

You have mainly two options:

* you use a linear regulator
* you use a switching regulator

(Please note that if you use the regulators already present on the UNO board - the 1117 and the LP2985 - you will use the first approach)

In the linear case, the regulator will drop the voltage by dissipating the excess power through heat. In this case, you will need to sum the currents:

Total current = 45mA + 30mA + 8mA = 83mA. Please note that if using a 9V battery, the 5V regulator will need to dissipate (9-5) * 83mA = 332mW of power (or slightly less if the 3.3V regulator directly interfaces to the 9V battery).

Now, according to the first datasheet I stumbled upon on google, this kind of battery have a capacity rated at 10mA, but at 100mA it loses about 40% of it. In fact, batteries tend to last less when the current increases (if you double the current you will get less than half of the time).

Considering so a mere 30% of loss at 83mA, your battery will last slightly above 4 hours and a half (550mAh\*70%/83mA)

Now, if you use a switching regulator to get 5V from the 9V (and then a linear one to get the 3.3V), you will have to generate 83mA at 5V. This means that, considering an efficiency of 85%, your battery will have to provide roughly 54mA (83mA\*5V/9V/85%).

According to the datasheet of the battery, this lower current reduces the degradation of the capacity to about 25%. The total uptime is consequently 7 hours and a half (550mAh\*75%/54mA).

If you put a second switching regulator to get the 3.3V, you will have \- 75mA at 5V, which with a 85% efficiency will become 49mA \- 8mA at 3.3V, and with a lower efficiency due to the very low current (let's say 75%) become 4.4mA

Total current from battery: 53.4mA (which can be considered equal to the case with just one switching converter.

Just one last note: please consider that a heated sensor like the one you have is not engineered for battery-powered devices, since a lot of power becomes heat. When you want to develop a real battery-powered sensor, you will need to go with a lower-power solution for both the microcontroller (and the board) and the sensor
> 1 votes

---
Tags: arduino-uno, sensors, power, hc-05
---


# Question
Title: Arduino IDE Board Selection for 32u4 on custom PCB
I am using a 32u4 on a custom PCB: 3.3v, 8MHz.

Are there any operational differences between using the Sparkfun ProMicro selection versus the Adafruit ItsyBitsy selection in IDE, since they are both for the 32u4?

# Answer
There should not be any difference, However you would need to load the ItsyBitsy bootloader instead of the ProMicro Bootloader. Personally, I use the Pro Micro Bootloader on my 32u4 Boards.

The only difference would be in the size of each bootloader, one may be smaller and give you more actual codespace. Other than that there should be no performance difference because your code runs independently of the bootloader.
> 1 votes

---
Tags: ide
---


# Question
Title: Make an Xbox 360 controller with an Arduino Uno
I was thinking of making a custom Xbox 360 controller (racing wheel) with the Arduino Uno plugged into the Xbox. How would I emulate the buttons/triggers on the original controller with the Arduino? Is there a library that can help me do this?

# Answer
Arduino Uno's do not have a native USB Connection. However, if you use an Arduino Leonardo or Pro Micro, you can use MHeironimus's Joystick library, However I am not sure if they are specifically compatible with the XBox.

https://github.com/MHeironimus/ArduinoJoystickLibrary
> 1 votes

---
Tags: arduino-uno
---


title: Problem to upload code

text:
# Question
Title: Problem to upload code
I tried to upload example code form arduino software and got error below. Two arduino attach with my computer running windows8.1. And These arduino using port 3 and port 4. The arduino using Port 3 is cordinator and arduino using port 4 is router. I doesn't understand why error show COM1 is not available. I am not using COM1.

```
Arduino: 1.6.0 (Windows 8), Board: "Arduino Uno"

Sketch uses 1,030 bytes (3%) of program storage space. Maximum is 32,256 bytes.

Global variables use 9 bytes (0%) of dynamic memory, leaving 2,039 bytes for local variables. Maximum is 2,048 bytes.

C:\Program Files (x86)\Arduino/hardware/tools/avr/bin/avrdude -CC:\Program Files (x86)\Arduino/hardware/tools/avr/etc/avrdude.conf -v -patmega328p -carduino -PCOM1 -b115200 -D -Uflash:w:C:\Users\Jiya\AppData\Local\Temp\build1135139715306090793.tmp/Blink.cpp.hex:i

avrdude: Version 6.0.1, compiled on Apr  3 2014 at 23:12:16
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "C:\Program Files (x86)\Arduino/hardware/tools/avr/etc/avrdude.conf"

         Using Port                    : COM1
         Using Programmer              : arduino
         Overriding Baud Rate          : 115200
avrdude: ser_open(): can't open device "\\.\COM1": The system cannot find the file specified.

avrdude done.  Thank you.

Problem uploading to board.  See http://www.arduino.cc/en/Guide/Troubleshooting#upload for suggestions.

Board at COM1 is not available
Board at COM1 is not available
Board at COM1 is not available
Board at COM1 is not available
Board at COM1 is not available

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

```

# Answer
> 4 votes
(I'm assuming you're using the official Arduino IDE here.)

Before uploading a sketch, you need to select which port you want to upload to. Within the Arduino IDE, click on the **Tools** menu and open the **Port** submenu. Click on the port your Arduino is connected to, such as COM3 or COM4.

You also need to ensure the appropriate board type is selected in **Tools -\> Board**.

You should then be able to and upload the sketch.

# Answer
> 1 votes
Did you try with code bender. In my experience, uploading through the code bender works all the time even if it fails with Arduino IDE

# Answer
> 1 votes
On windows systems, `COM1` is usually the system port, so you shouldn't use that for programming.

Try updating device drivers by going to *device manager* and right click on the `COM3` and `COM4` icons for the arduino boards.

If this keeps persisting then try reinstalling the ide and restarting your computer

# Answer
> 1 votes
If all the other solutions don't work and you are on a Linux distro try `sudo arduino` and that should work, or follow these instructions: Getting Started With Arduino on Linux (Ubuntu)

# Answer
> 0 votes
Experience has taught me twice... to make sure you use the right cables. I used a spare cable that ended up being a power brick charging cable, i.e. no data lines. Make sure you use a decent USB cable - preferably one that asks you question on a cellphone when connected to a PC, so you know all the internal wires are there and working.

# Answer
> -1 votes
your kernel need to provide support to USB ACM.

```
CONFIG_USB_ACM=y

```

you can look for it at:

```
$ zcat /proc/config.gz | grep CONFIG_USB_ACM
CONFIG_USB_ACM=y

```

---
Tags: arduino-uno, uploading
---


# Question
Title: Using TFT with hardware SPI and speaker with tone()
I want to make a device with a TFT screen and a speaker that uses the builtin `tone()` function. However, according to the documentation, using `tone()` interferes with PWM on pins 3 and 11. On the Arduino Uno, the MOSI pin is default on pin 11, and it seems like defining another pin for MOSI disables hardware SPI, which, according to the module page, slows down the device. As well, the examples are written for hardware SPI and the SD card is only available with hardware SPI. What should I do here? Is there a way to redefine the default MOSI pin without disabling hardware SPI?

# Answer
> 1 votes
You are getting your wires crossed in your brain and imagining a problem where there is none.

Both PWM on pins 3 & 11, and `tone()`, use the Timer2 device. Only one of those can use Timer2 at a time, and they both want to use it in different ways, so there is a conflict between them.

Timer2 has nothing to do with SPI. So `tone()` has no effect on SPI, and nor does PWM have any effect on SPI (other than the fact that only one can come out of pin 11 at any time), since there is no internal resource conflict.

---
Tags: arduino-uno, spi, tft
---


# Question
Title: Which board's bootloader is on the microcontroller?
Currently I'm doing a project that needs an Atmega2560 standalone MCU. After I burned the bootloader with this method: http://www.gammon.com.au/bootloader . I set the Board as "Arduino/Genuino Mega or Mega 2560" and the Processor as "ATmega2560 (Mega 2560)". But, it just stuck at "uploading" and this message pop up: "avrdude: stk500v2\_ReceiveMessage(): timeout". So, I'm guessing I'm choosing a wrong board. Is there a way to check which board to select in the Tool tab?

# Answer
It depends on which bootloader you loaded, when you write your bootloader with AVRDude you specify the hex file, Which Hex file did you specify?
> -1 votes

---
Tags: atmega2560
---


# Question
Title: Question about browsing the web using arduino
I have got an arduino, a TFT touch screen and a WIFI module. The question is can i visit a specific web site and display it on that screen and interact with it.

# Answer
> 3 votes
You just have to install Chromium and call the desired page.

No, just kidding. I don't think you will ever fit a parsing and rendering engine on an arduino. Chromium for example takes about 150MiB of storage and depending on the opened page hundreds of MiB of RAM. Both of which is multiple orders of magnitude larger than the Arduino has to offer.

You might want to consider going to use a SBC like the Raspberry Pi for such usecase.

Alternatively you can query the data over network and write your own rendering code for the TFT display.

---
Tags: arduino-uno
---


# Question
Title: Multiple Slaves(Arduino Mega 2560) response over SPI
I have problem creating Setup which have :

1. A SPI Master (Arduino Mega 2560)
2. SPI slaves (Arduino Mega 2560) ,which responds to the Masters request by sending float data (or Rather a 2D float array ,but lets just go step by step).

So,I initially started with 3 Arduinos in total (going step by step):

* Arduino\_1 :Master
* Arduino\_2 :Slave\_1
* Arduino\_3 :Slave\_2

I used ICSP pins for connection and Digital pin 53 (as mentioned in Nick Gammon's page :http://www.gammon.com.au/spi or How do you use SPI on an Arduino? ,they are good resources to start with SPI) and 10 for SS.

MY Master codes looks like:

```
#include <SPI.h>
#define SS_Slave_2 10

void setup ()
{
 Serial.begin (9600);//115200

 pinMode(SS,OUTPUT);
 pinMode(SS_Slave_2,OUTPUT);
 pinMode(MOSI,OUTPUT);
 pinMode(SCK,OUTPUT);

 digitalWrite(SS, HIGH);  // ensure SS stays high for now
 digitalWrite(SS_Slave_2, HIGH);

 digitalWrite(SCK,LOW);
 digitalWrite(MOSI,LOW);

 // Put SCK, MOSI, SS pins into output mode
 // also put SCK, MOSI into LOW state, and SS into HIGH state.
 // Then put SPI hardware into Master mode and turn SPI on
 SPI.begin ();

 // Slow down the master a bit
 SPI.setClockDivider(SPI_CLOCK_DIV8);
}

byte transferAndWait (const byte what)
{
  byte a = SPI.transfer (what);
  delayMicroseconds (100);
  return a;
}

union first_union
{
 float f;
 byte b[4];
}data;

float yaw, pitch, roll, alt, temp;
float yaw_2, pitch_2, roll_2, alt_2, temp_2;

void loop (void)
{
 digitalWrite(SS_Slave_2, HIGH);
 digitalWrite(SS, LOW);

 //YAW
 transferAndWait ('Y');
 data.b[0] = transferAndWait ('1');
 data.b[1] = transferAndWait ('2');
 data.b[2] = transferAndWait ('3');
 data.b[3] = transferAndWait ('Z');
 yaw = data.f;

 //PITCH
 transferAndWait ('P');
 data.b[0] = transferAndWait ('1');
 data.b[1] = transferAndWait ('2');
 data.b[2] = transferAndWait ('3');
 data.b[3] = transferAndWait ('Z');
 pitch = data.f;

 //ROLL
 transferAndWait ('R');
 data.b[0] = transferAndWait ('1');
 data.b[1] = transferAndWait ('2');
 data.b[2] = transferAndWait ('3');
 data.b[3] = transferAndWait ('Z');
 roll = data.f;

 //TEMP
 transferAndWait ('T');
 data.b[0] = transferAndWait ('1');
 data.b[1] = transferAndWait ('2');
 data.b[2] = transferAndWait ('3');
 data.b[3] = transferAndWait ('Z');
 temp = data.f;

 //ALT
 transferAndWait ('A');
 data.b[0] = transferAndWait ('1');
 data.b[1] = transferAndWait ('2');
 data.b[2] = transferAndWait ('3');
 data.b[3] = transferAndWait ('Z');
 alt = data.f;

 digitalWrite(SS, HIGH);
 digitalWrite(53, HIGH);
 digitalWrite(SS_Slave_2, HIGH);
 Serial.println("Slave_01: Start");
 Serial.print("yaw:");
 Serial.println(yaw, 4);
 Serial.print("pitch: ");
 Serial.println(pitch,4);
 Serial.print("roll:");
 Serial.println(roll,4);
 Serial.print("alt: ");
 Serial.println(alt,4);
 Serial.print("temp: ");
 Serial.println(temp,4);
 Serial.println("Slave_01: End");

 digitalWrite(SS, HIGH);
 digitalWrite(53, HIGH);
 digitalWrite(SS_Slave_2, LOW);

 //YAW
 transferAndWait ('Y');
 data.b[0] = transferAndWait ('1');
 data.b[1] = transferAndWait ('2');
 data.b[2] = transferAndWait ('3');
 data.b[3] = transferAndWait ('Z');
 yaw_2 = data.f;

 //PITCH
 transferAndWait ('P');
 data.b[0] = transferAndWait ('1');
 data.b[1] = transferAndWait ('2');
 data.b[2] = transferAndWait ('3');
 data.b[3] = transferAndWait ('Z');
 pitch_2 = data.f;

 //ROLL
 transferAndWait ('R');
 data.b[0] = transferAndWait ('1');
 data.b[1] = transferAndWait ('2');
 data.b[2] = transferAndWait ('3');
 data.b[3] = transferAndWait ('Z');
 roll_2 = data.f;

 //TEMP
 transferAndWait ('T');
 data.b[0] = transferAndWait ('1');
 data.b[1] = transferAndWait ('2');
 data.b[2] = transferAndWait ('3');
 data.b[3] = transferAndWait ('Z');
 temp_2 = data.f;

 //ALT
 transferAndWait ('A');
 data.b[0] = transferAndWait ('1');
 data.b[1] = transferAndWait ('2');
 data.b[2] = transferAndWait ('3');
 data.b[3] = transferAndWait ('Z');
 alt_2 = data.f;

 digitalWrite(SS_Slave_2, HIGH);
 digitalWrite(SS, HIGH);
 digitalWrite(53, HIGH);
 Serial.println("Slave_02: Start");
 Serial.print("yaw_2:");
 Serial.println(yaw_2, 4);
 Serial.print("pitch_2: ");
 Serial.println(pitch_2,4);
 Serial.print("roll_2:");
 Serial.println(roll_2,4);
 Serial.print("alt_2: ");
 Serial.println(alt_2,4);
 Serial.print("temp_2: ");
 Serial.println(temp_2,4);
 Serial.println("Slave_02: End");

 delay(500);
}

```

And Slave Code goes like:

```
#include <SPI.h>

volatile byte command;
//Set the default object
float ypr[3];
float alt = 0;
float temp = 0;

void setup()
{
  Serial.begin(9600);

  // turn on SPI in slave mode
  SPCR |= _BV(SPE);

  // have to send on master in, *slave out*
  pinMode(MISO, OUTPUT);
  pinMode(MOSI,INPUT);
  pinMode(SCK,INPUT);
  pinMode(SS,INPUT);

  // turn on interrupts
  SPCR |= _BV(SPIE);

}

union first_union
{
    float f;
    byte b[4];
 }data;

ISR (SPI_STC_vect)
{
  byte c = SPDR;

  command = c;

  switch (command)
  {
   case 'Z':
   data.f = 0;
   SPDR = 0;
   break;
   case 'Y':
   data.f = ypr[0];
   SPDR = data.b[0];
   break;
   case 'P':
   data.f = ypr[2];
   SPDR = data.b[0];
   break;
   case 'R':
   data.f = ypr[1];
   SPDR = data.b[0];
   break;
   case 'A':
   data.f = alt;
   SPDR = data.b[0];
   break;
   case 'T':
   data.f = temp;
   SPDR = data.b[0];
   break;
   case '1':
   SPDR = data.b[1];
   break;
   case '2':
   SPDR = data.b[2];
   break;
   case '3':
   SPDR = data.b[3];
   break;
  }
}

void loop()
{
  Serial.println("loop() starts");
  ypr[0] = -1.1111;
  ypr[1] = -2.2222;
  ypr[2] = -3.3333;

  alt = -4.4444;
  temp = -5.5555;
  Serial.println("loop() ends");
}

```

The above codes (Master and Slave) are basically modified and adopted from Joseph Roberts' Question on https://stackoverflow.com/questions/20409799/sending-floats-from-an-arduino-to-another-arduino-over-spi-using-a-union , purely for learning purpose only. (I modified it a bit and now similar codes works well for 1 SPI Master and 1 SPI slave Arduino setup).

//Note:Same Slave code uploaded to both the slaves.

OUTPUT:

```
Slave_01: Start
yaw:-1.1111
pitch: -3.3333
roll:-2.2222
alt: -4.4444
temp: -5.5555
Slave_01: End
Slave_02: Start
yaw:0.0000
pitch: 0.0000
roll:-2.2222
alt: 0.0000
temp: 0.0000
Slave_02: End
Slave_01: Start
yaw:-1.1111
pitch: -3.3333
roll:-2.2222
alt: -4.4444
temp: -5.5555
Slave_01: End
Slave_02: Start
yaw:0.0000
pitch: 0.0000
roll:0.0000
alt: 0.0000
temp: 0.0000
Slave_02: End
Slave_01: Start
yaw:-1.1111
pitch: -3.3333
roll:-2.2222
alt: -4.4444
temp: -5.5555
Slave_01: End
Slave_02: Start
yaw:0.0000
pitch: 0.0000
roll:0.0000
alt: ovf
temp: 0.0000
Slave_02: End
Slave_01: Start
yaw:-1.1111
pitch: -3.3333
roll:-2.2222
alt: -4.4444
temp: -5.5555
Slave_01: End
Slave_02: Start
yaw:0.0000
pitch: 0.0000
roll:0.0000
alt: 0.0000
temp: ovf
Slave_02: End

```

As we can see from output ,I got output from one of the slave only (Arduino\_2:Slave\_1) ,the other gave unreliable/garbage output .That is the problem ,I don't get it where the zeros for 2nd Slave comes from ?And how to correct it.Did I missed something ?

Even the page(http://www.gammon.com.au/spi) by Nick Gammon on SPI ,lacks details on Multiple Slave Arduino SPI connection (Although It is really a good place to start with SPI on Arduino, no offense ).I know my Question is similar to Slaving multiple Arduinos via SPI ,but still the answer there doesn't helps much. I almost spend entire Night searching for the solution.

I even tried things like changing the SS for the Slaves ,but still only Arduino\_2 gave the correct results and Arduino\_3 does not.

And yes ,Arduino\_3 is not damaged (It works well in 1master\_1slave setup). And all the Arduinos has common GND .

Any constructive criticisms are also welcomed.

Warm Regards

SV

# Answer
I know this item is quite old, and I'm also trying to implement multiple Arduino SPI slaves, and looking at all documentations, my understanding is that the SS is not automatically managed in the slave Arduinos. From that, unsure about the wiring of your 1 master/2 slaves Arduinos that can alter the results as shown above, it looks to me your slave code does not manage the SS from two aspects :

1 - I don't see when you identify in your ISR reception procedure if the applicable slave is enabled ( aren't both modules processing the message all the time in this configuration, I suspect they both do )

2 - Also about the SS event management in the slave module, pinMode(MISO, OUTPUT); should only be set when SS is low, otherwise you may have faulty signal with two slaves having MISO as outputs. My understanding is that only one slave at a time should have it as an output ( hence my question about wiring )

Hope it helps,
> 1 votes

---
Tags: arduino-mega, spi
---


# Question
Title: Trouble understanding connection diagram
The image shows a way of connecting an optocoupler. My problem comes from how the emitter (Q1 in the image) is connected. The emmiter goes to a resistance going to ground and to v\_out. When i try to add this to my circuit the pin that is suppost to be receiving data, the v\_out pin goes silent. If i remove the resistance going from the emitter to ground and just have vout to the emitter everything works fine.

# Answer
> 2 votes
Normally you would connect the ground (0V) side of resistor R2 to the ground of the Arduino and the other side of R2 (which is also connected to the emitter of Q1) to an input pin of the Arduino. When the LED is energized by pressing the button it sends light to the fototransistor. Because of this the phototransistor starts conducting current (I1) which causes a voltage drop I1xR2 over R2. When this voltage is high enough the input pin will show a "1" (or HIGH) when read out. "High enough" means \>= 0.7 x Vcc, where Vcc is the voltage of the powerline to the Arduino.

The website from which the picture in your question originates (here) has an extensive article about the working and implementation of optocouplers. It also shows examples of alternative circuits.

---
Tags: arduino-uno
---


title: FreeRTOS Timer Use

text:
# Question
Title: FreeRTOS Timer Use
I use Timer1 on an UNO to generate a 38KHZ signal for IR communications. It all works as expected.

BUT, I need to convert the code to run as a task under FreeRTOS, and can't seem to find any details about which HW timers are available for the UNO or MEGA.

Also, as long as I'm asking about FreeRTOS, is it reliable for the timer tick to be changed from the 15ms that I think is standard to something faster?

# Answer
FYI, I found my own answer by looking into the FreeRTOS source code.

FreeRTOS uses timer0, timer1, timer2 and timer3 according to the source code. I assume that it uses the timers as required based on CPU type and capability.
> 1 votes

---
Tags: arduino-uno, arduino-mega, arduino-nano
---


# Question
Title: library mixing log4Esp & Syslog for esp8266
has someone finaly made a new lib combining log4esp and syslog cf : SyslogAppender for log4esp thanks:!

# Answer
> 0 votes
I did a rather **dirty** *(but efficient for my purpose)* job : While creating a new logger I declared and attached to it a log4esp formatter in which I sent ... syslog messages

*dirty dirty dirty*

my config constants are : `SYSLOG_SERVER, SYSLOG_PORT, DEVICE_HOSTNAME, APP_NAME`

My `logger.h`:

```
#ifndef Logging_H
#define Logging_H
#include <Log4Esp.h>
namespace Logging
{
void setup() ;
log4Esp::Logger Logger(const char *name);
}
#endif

```

my `logger.cpp`:

```
#include <Log4Esp.h>
#include <Syslog.h>
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
#include "Logger.h"
#include "config.h"
namespace Logging
{
// A UDP instance to let us send and receive packets over UDP
WiFiUDP udpClient;
// Create a new syslog instance with LOG_KERN facility
Syslog syslog(udpClient, SYSLOG_SERVER, SYSLOG_PORT, DEVICE_HOSTNAME, APP_NAME, LOG_KERN);
//new logger
log4Esp::Logger Logger(const char *name)
{
  //new instance
  log4Esp::Logger logger = log4Esp::Logger(name);
  //minimal level
  logger.addLevelToAll(log4Esp::Appender::LOG_LEVEL);
  // formatting for serial logging
  logger.addFormatterToAll([name](Print & output, log4Esp::Appender::Level level, const char *msg, va_list * args) {
    ////////////////////////////////////////
    // serial log format
    // output uptime of this program in milliseconds
    output.printf("%10d", millis());
    output.print(log4Esp::Appender::DEFAULT_SEPARATOR);
    // output free heap space
    output.printf("(%d)", ESP.getFreeHeap());
    output.print(log4Esp::Appender::DEFAULT_SEPARATOR);
    // output logger name
    output.printf("%-4s", name);
    output.print(log4Esp::Appender::DEFAULT_SEPARATOR);
    // output log level
    output.print(log4Esp::Appender::toString(level, true));
    if (level <= log4Esp::Appender::Level::ERROR) {
      output.print("***");
    } else {
      output.print("   ");
    }
    output.print(log4Esp::Appender::DEFAULT_SEPARATOR);
    // determine buffer length for formatted data
    size_t length = vsnprintf(NULL, 0, msg, *args) + 1;
    char buffer[length];
    // output formatted data
    vsnprintf(buffer, length, msg, *args);
    output.print(buffer);
    ////////////////////////////////////////
    // Stop here if wifi is down
    if (WiFi.status() != WL_CONNECTED)
      return;
    ////////////////////////////////////////
    // format for syslog
    uint16_t slevel;
    switch (level) {
      case log4Esp::Appender::Level::FATAL    :
        slevel = LOG_CRIT;
        break;
      case log4Esp::Appender::Level::ERROR    :
        slevel = LOG_ERR;
        break;
      case log4Esp::Appender::Level::WARNING  :
        slevel = LOG_WARNING;
        break;
      case log4Esp::Appender::Level::VERBOSE  :
        slevel = LOG_INFO;
        break;
      case log4Esp::Appender::Level::TRACE    :
      default:
        slevel = LOG_DEBUG;
        break;
    }
    syslog.logf(slevel, "%10d|(%d)|%-4s|%s", millis(), ESP.getFreeHeap(), name, buffer);
  });
  return logger;
}
void setup() {

}
}

```

---
Tags: esp8266
---


# Question
Title: Arduino button-controlled LED with AVR Assembly
I am trying to make a simple button-controlled LED, but I cannot seem to get any input from the button that would change the state of the LED. In other words, the program keeps branching to lighton, keeping the LED always on, regardless of the button state. I am using an Elegoo Uno R3.

Schematic:

Code:

```
.text
.global setup
.global loop

setup:
  ser r16
  sts DDRB, r16
  ldi r16, 0b00000000
  sts DDRD, r16
  ldi r16, 0b00100000
  sts PORTB, r16
  clr r16
  sts PORTD, r16

loop:
  lds r20, PORTD
  cpi r20, 0x00 ; PORTD always equal to 0x00 ?
  breq lighton
  clr r16
  sts PORTB, r16
  rjmp loop
lighton:
  ldi r16, 0b00100000
  sts PORTB, r16
  rjmp loop

```

Edit: I also implemented the method in https://stackoverflow.com/questions/39361410/creating-a-toggle-switch-in-avr-assembly to create a short delay with `r18` instead of `del`, but it didn't help; see revised loop method below:

```
loop:
  cpi r18, 0
  brne dec_jmp
  lds r20, PORTD
  cpi r20, 0x00 ; PORTD always equal to 0x00 ?
  breq lighton
  clr r16
  sts PORTB, r16
  ldi r18, 250
  rjmp loop
lighton:
  ldi r16, 0b00100000
  sts PORTB, r16
  ldi r18, 250
  rjmp loop

dec_jmp:
  dec r18
  rjmp loop

```

With delay:

```
lighton:
  ldi r16, 0b00100000
  sts PORTB, r16
  call delay150
  ldi r16, 0b00000000
  sts PORTB, r16
  call delay150
  rjmp loop

```

# Answer
> 1 votes
Fixed it with the below code for the loop:

```
loop:
  sbic PIND, 2
  call nopressb
  ; button pressed
  ldi r16, 0b00100000
  out PORTB, r16
  rjmp loop
nopressb:
  ldi r16, 0b00000000
  out PORTB, r16
  rjmp loop

```

---
Tags: arduino-uno, led, button, assembly
---


# Question
Title: ATMEGA 328P Standalone on 3.3v controlling a servo
I'd like to use an ATMEGA 328P on standalone mode powered by a 3.3V supply source to control a servo (which is powered by 4 AA batteries, resulting in 6V). My question is: do I have to convert the PWM signal's peak amplitude to 5V for it to work? Or will the servo work just fine with a 3.3V amplitude signal? My servo's datasheet says its operation voltage ranges from 4.8V to 6V, but I'm not sure if that applies only to its power source or if it has implications on the control signal as well.

Servo's datasheet: https://www.donkeyhoute.com/UploadFiles/2014916134245511.pdf

Also, since I will be using an 8 MHz external clock, do I have to change anything in my Arduino sketch regarding the PWM signal frequency, or do I just set the board to the 'Lilypad' option when burning the bootloader and keep the sketch the way it is now?

# Answer
> do I have to convert the PWM signal's peak amplitude to 5V for it to work?

There is insufficient information to determine that. It may, or it may not. You'll have to try it and see.

> since I will be using an 8 MHz external clock, do I have to change anything in my Arduino sketch regarding the PWM signal frequency

If the board you select is 8MHz then no, you don't need to change anything. The fact you have selected the right frequency for the board means that all the timing calculations the Arduino API does will be correct.
> 0 votes

---
Tags: servo, atmega328
---


title: SD card initialization failed

text:
# Question
Title: SD card initialization failed
i use the example sketch of SD card presented in Arduino IDE. It runs well, but when i reboot my Adruino Mega by the reset button, the SD card initialization fails. To fix it, i need to remove the SD card, insert it in his socket and reboot the board again. Then, SD card initialization is OK. How can i update my code to try initialize the SD card until initialization is OK? Also, can you explain me where come this bug ?

See the example SD card setup on the Arduino Mega:

```
Serial.print("Initializing SD card...");

  if (!SD.begin(53)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

```

# Answer
I just add an external 9V power supply to my Arduino Mega. The SD device is power by arduino 5V pinout.
> 0 votes

---
Tags: arduino-mega, sd-card
---


title: Confusion between switch and if?

text:
# Question
Title: Confusion between switch and if?
Following code is to control two LEDs for on and off by two dedicated push buttons. I have used `if()` for each condition and I was not comfortable writing this code, as I could feel, it can be better with `switch...case`, because if we increase more buttons, it will be a messy solution for that. However, I couldn't get optimized `switch...case` for it. Can some one suggest the nice switch transformation for this `if()`.

```
int buttonState1 = 0; // variale for reading pushbutton1 status
int buttonState2 = 0;  // variable to reading push button2 status
//set pin numbers
int button1 = 2; // push button no. 1
int button2 =3;  // push button no. 2
int Led1 = 4; // led 1
int Led2 =5;  // led 2

void setup() {
  Serial.begin(9600); // serial begins at 9600
  // defining pin modes for led and buttons
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode (Led1, OUTPUT);
  pinMode (Led2, OUTPUT);
};

void loop() {
 buttonState1 = digitalRead(button1); // reading pushbutton 1 in the variable buttonstate1
 buttonState2 = digitalRead(button2); // reading pushbutton 2 in the variable buttonstate2
 // condintion for led 1 ON, if pushbutton1 is pressed
 if (buttonState1 == HIGH) {
  digitalWrite(Led1, HIGH);
  }
  // condition for led2 ON, if pushbutton2 is pressed.
 if (buttonState2 ==HIGH){

  digitalWrite(Led2, HIGH);

  }
  // both led are off if no push button is pressed
  else{
    digitalWrite(Led1, LOW);
    digitalWrite(Led2, LOW);
  }

};

```

# Answer
> 1 votes
A switch case does not seem like it would be an effective way of controlling your LEDs. It would make more sense (and would probably be easier) to create a function instead to control the LEDs.

```
void buttonToggle(int buttonPin, int ledPin) {
  int buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

```

You would then need to replace your `if` statement with calls to the function (EX `buttonToggle(Button1, Led1);`

# Answer
> 1 votes
If you want to use a dedicated push button for every led, then it is not handy to use a switch .. case statement. This is because first you have to construct the switch-variable based on the readings of the switches before you can use it (the switch variable) for de case statements.

When you combine the reading of the switches with the setting of the leds you get a nice Loop-section:

```
    void loop() {
      digitalWrite(Led1, digitalRead(button1));
      digitalWrite(Led2, digitalRead(button2));
      digitalWrite(Led3, digitalRead(button3));
      digitalWrite(Led4, digitalRead(button4));
      digitalWrite(Led5, digitalRead(button5));
      // and so on
    }

```

---
Tags: arduino-uno, button, loop
---


title: Addressable RGB LED Strip

text:
# Question
Title: Addressable RGB LED Strip
I am looking to create an addressable RGB LED strip circuit with my NodeMCU v3 Wi-Fi. I am looking at a strip of 150 RGB LEDs, 5 meters long, so 30 LED/m. But I am only going to use about 50 LEDs for the backlight on my desk. My understanding is that if each RGB LED pulls 60 mA on full white, and I'm running 50 LEDs, then I will need at least 60 mA x 50 LEDs = 3000 ms (3 A) of power to sufficiently light all of them. I am thinking about upgrading this in the future, and so I am thinking about going with a 5V 10A power supply. Am I okay to go with a 5 V 10 A power supply? Or will this be too much current for the system.

# Answer
Each will only draw as much current as they normally would from a 5 volt supply. If you had a 5 volt 100 amp supply they still would not draw any more.

The amount of excess current a power supply can deliver doesn't change what the load will take from that supply.

For example, if you had a 5 volt supply with a 1 ohm resistor as a load the current would be 5 amps. 5 volts / 1 ohm = 5 amps. And if you had a 5 volt 10 amp supply the current would still be 5 amps. Same with a 20 amp supply. As long as the voltage is the same, going up in current capacity is safe.

(LEDs are not resistors but with this application it is much the same)

When you do wire up the LED strips make sure you are using thick enough wires.
> 1 votes

---
Tags: arduino-uno, nodemcu, rgb-led, current
---


# Question
Title: Idenitifying pins in my code. which pin is which?
I'm new to arduino and knnow nothing about electronics. How do I idenitify which pin is which in the coding? eg- I have a pin on my esp8266 "D3"... how would I reference that in the code?

# Answer
> 1 votes
If you select the right board in Tools menu, you can use constants corresponding to labels.

```
pinMode(D3, OUTPUT);
digitalWrite(D3, LOW);

```

---
Tags: esp8266
---