Works fine powered from USB works bad powered externally

I have soldered very simple project on nano v3 includes i2c oled 128x32, pushbutton with certain sequence to change display data and isolated optocoupler used as 220 to 5V adapter (the project should count numbers of connections to 220V).
When I power it from USB it works fine.
But I need to power it from 220V, so I use 220 to 12V adapter. The problem is when I use it the device is working bad. It means that sometimes (approximately 10-15%) of tests fail. For example, oled display hangs on or microcontroller hangs on or event from optocoupler isn't being counted.
I am trying to solve this for more than a week. I have restarted the soldering from scratch 4 times. During my trials I've noticed that if I connect the USB oscilloscope to 12V output of power supply adapter (Vin on nano) the device works fine even if I connect only ground probe of the scope. So the signal on the scope looks fine.
Also I tried to use 7805 and to connect to 5V directly but the result was the same.
Maybe anyone could advise anything generally?
Thank you in advance.

I think that we need a schematic of the project showing all components, their part numbers and/or values and all power supplies.

What are the specifications of the external power supply (output voltage, maximum current capability)?

That's a good move, you should be powering it from 5V to the 5V pin, or better still buy a 5V power supply with a USB connector and connect that to the USB socket. Alternatively use and old phone charger with the correct USB cable.

I'm sorry for inconvenience that it is in Russian :

AC/DC converter
OLED display:

Yes but I need to stay the USB port available in this project.

A picture of the underside of the board showing a good look at the soldering job.

A schematic.

Which module is the 5V regulator? The one in the Center or the one to the right?

5V can be fed into the boards 5V pin from the 5V regulator.

The issue reads like a grounding issue.

Thank you.
About 5V regulator - I mentioned that I tried to use it but after I've seen it didn't help I removed It.
Now the Arduino powered from 220 to 12V adapter (on the right). Its output connected to Vin.
Yes, I also think it is grounding issue. But I don't know how to solve it...

What is an Optron ? Can you post a link ?

Power the unit off.

Disconnect power.

Set your Ohm meter to detect continuity.

Put the black lead on the USB connector cover on the CPU. Use the red lead to trace out all the ground connections, keeping the black lead on the USB connector cover.

OK. Thanks. It is then simply to detect the presence of mains voltage via an opto-coupler.
Then post the code you are using.

Thank you. Checked, it's ok.

Read the forum guidelines to see how to properly post code and some hints on how to get the most from this forum.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

#include <EEPROMex.h>

#define OLED
#define OPTRON

#ifdef OLED
#include <Adafruit_SSD1306.h>
const uint8_t oled_width = 128;
const uint8_t oled_height = 32;
const uint8_t oled_address = 0x3C;
const int8_t  oled_rst = -1;
const int16_t x_cursor_line1 = 0;
const int16_t y_cursor_line1 = 0;
const int16_t x_cursor_line2 = 0;
const int16_t y_cursor_line2 = 16;
Adafruit_SSD1306 display(oled_width, oled_height, &Wire, oled_rst);

const int password_value = 37;
const int8_t event_pin = 7;
const int8_t rst_pin = 4;
const int eeprom_address = 0;
const unsigned long debounce_delay = 100;
const unsigned long btn_press_interval = 200;
const unsigned long event_min_duration = 1300;
const unsigned long event_min_duration = 300000;//Минимальная длительность учитываемого события 5 min(ms)
const unsigned long long_press_duration = 10000;
const unsigned long master_mode_timeout = 2000;
uint32_t events_count = 0;
int last_switch_state = LOW;
int last_rst_state = LOW;
int last_master_state = LOW;
#ifdef OPTRON
const int event_state = LOW;
const int event_state = HIGH;

void setup() {
  pinMode (event_pin, INPUT_PULLUP);
  pinMode (rst_pin, INPUT_PULLUP);
  display.begin(SSD1306_SWITCHCAPVCC, oled_address);

void loop() {

void event_listener() {
  int switch_state = digitalRead(event_pin);
  if (switch_state != last_switch_state) {
    unsigned long switch_change_started = millis();
    while (digitalRead(event_pin) == event_state) {
      if ((millis() - switch_change_started) > event_min_duration) {
        user_mode(); //выведение на дисплей после каждого события
  last_switch_state = switch_state;

void rst_btn_listener() {
  int rst_state = digitalRead(rst_pin);
  if (rst_state != last_rst_state) {
    unsigned long press_btn_started = millis();
    while (digitalRead(rst_pin) == LOW) {
      if ((millis() - press_btn_started) > long_press_duration) {
  last_rst_state = rst_state;

void user_mode() {
  events_count = read();
  // display_text("USER MODE");

void display_text(String text) {
  display.setCursor(x_cursor_line1, y_cursor_line1);

void display_num(uint32_t num) {
  display.setCursor(x_cursor_line2, y_cursor_line2);

void rst_mode(byte btn_pressed_counter) {
  display_text("MASTER MODE");

void master_mode() {
  int btn_pressed_counter = 0;
  unsigned long last_btn_pressed_time = millis();

  while (btn_pressed_counter != password_value) {
    if ((millis() - last_btn_pressed_time) > master_mode_timeout)
    if (rst_btn_toggled()) {
      last_btn_pressed_time = millis();

  if (btn_pressed_counter == password_value) {
    display_text("MEMORY RESET");


bool rst_btn_toggled() {
  bool toggled = false;
  int rst_state = digitalRead(rst_pin);
  if (rst_state != last_master_state) {
    unsigned long press_btn_started = millis();
    if (rst_state == LOW)
      toggled = true;
  last_master_state = rst_state;
  return toggled;

void increment() {
  events_count = read();
  events_count += 1;

void write(uint32_t data) {
  EEPROM.writeLong(eeprom_address, data);

uint32_t read() {
  return EEPROM.readLong(eeprom_address);

void clear() {
  EEPROM.writeLong(eeprom_address, 0);