First ever project looking for efficiency improvements and guidance

Hello there!

I am starting my journy in the world of electronics and I would say I am closing on half way through my first project.
I am creating this post so I can get some tips on how to improve my schematic design (I tried to always keep in mind what is said in the guidelines for circuit design from the FAQ section) and get some validation on the connections of my circuit (from someone who is not a potato like me :grin:) because honestly I would like to know if what I “designed” is at least “functional” so I can order some components and begin testing and prototyping.

A quick rundown of the project:

  • supply is 230Vac, gets converted to 5Vdc (with RAC03-05SK) and powers the rest of the circuit;
  • microcontroller (ATtiny84A) has 3 input signals of 230Vac (and one 5V pushbutton) that get sensed by HCPL-3700;
  • if some conditions are met within the microcontroller, the output PB1 is high and through the transistor BC337 a 5V relay gets triggered and a 230Vac signal is sent back to a exterior circuit.

My actual questions:

  • Any books you would recommend on electronics/electricity? Because after programming the microcontroller and starting to actually build a circuit, I felt completly lost and clueless.
  • To get the minimal current consumption how would I calculate the most appropriate value for the several resistors?
  • Does the setup of the transistor+relay even make sense?

(I would have linked datasheets to the components that don’t have one but as a new user I can only use 2 links per post)

Thank you for reading :grinning:

1 Like

For a first project I would suggest that you look at the option to use an external 5V ready made power supply (“wall wart”) to avoid all the pitfalls of designing mains voltage on the PCB / in your encolsure.

For 3W this is certainly the easiest and safest way.


Hi hmk, unfortunatly that’s not really an option for me, using the rac03-05sk is almost mandatory. Nevertheless, I will keep that in mind for later low power projects as you recommended.
Thank you very much :slightly_smiling_face:

It might be more elegant to put K1 and D2 in place of R8 and have PB1 negated (or have the contacts swapped, and maybe also for PB0).

With AC mains projects I like to draw dashed lines on the schematic to show the safety barriers.
I notice that you have Gnd connecting to J2 and then a connection from J2 to J1. You probably want to float Gnd safely away from AC, the PSU module is Class II insulated.
There seem to be 3 optoisolators and a relay across the barrier.
Organising the schematic to show the mains barrier more clearly is essential in the commercial world so that a safety test lab can understand your circuit

Where is signal “COM” going from J3?

Hi franzee, was this what you where suggesting?

About PB0, I didn’t really understand what you where suggesting, I am really very noob, sorry and thank you :grinning:

Still wrong, Q1 would short the power
This is why we are concerned about a beginner starting off with a mains powered design. Mains kills


Is there any material/book/playlist you would recommend? I do feel like I don’t know most of the stuff I am working with

1 Like

Makes sense, I will work on that! Thank you!
The COM signal isn’t going anywhere, nice catch!

I understand what you are saying, but I do plan to simulate everything before I actually do anything. I do know electricity is no joke and I appreciate your concern. The only reason I haven’t simulated anything yet is that I am still trying to understand how to create the spice models for the costum components I am using (RAC03-05SK, HCPL-3700 and BC337)

Have a read through this site for more info on powering relays from a transistor.

Both configurations you have are incorrect.

I would also add a 100nF decoupling cap to the 5V near the ATtiny.

What function does J2 serve?

As others have suggested as a beginner please stay away from mains voltages until you are familiar with electronics. Best case if mains is all wired incorrectly you will blow up your circuit and hopefully mains will trip. Worst case you could shock yourself and if mains doesn’t trip it could be fatal.

Do not take this lightly.


It’s probably best to first take a look at how others are doing it. For example this relay or LED.

And for this particular project, it might be more encouraging to first prototype this circuit on a breadboard with a battery and then start simulating it (I know it’s the wrong order but depending on the simulator you’re using it might be more satisfying to have it work first).

Oops, I just now see that the relay is switching 230V. Such a relay should definitely not be on a beginner’s board (unless one’s used to work with vacuum tubes/valves :wink:). Try to use a finished product for that part with plugs/sockets/connectors assembled, same as already suggested for the power supply.

You need programmer pins for the microcontroller.

Something odd about the reset circuit too. PB2 is just tied to Gnd, so the button won’t work. Why isn’t it simply pulling down !RESET?

This seems to depend on AC as the three optos are unusual devices intended for AC measurement.
You should be aware that AC power is dirty and optos won’t last long without some sort of surge protection.

You need an experienced engineer to work with you on this.

Have a close look at the circuit near the text “reset button”.

First, PB2 is always connected to GND, regardless of the use of SW1 or placing R6 on the PCB.
And apparently the reset pin of U1 is PB3 and not PB2.

I totally agree with the first remark made by hmk. Especially for beginners keeping mains voltage far away from the PCB is a good idea. The simplest way to do that is to put your RAC03-05SK module in a plastic box and then only route the +5 to your PCB. Do this at least until your PCB has been fully debugged and working. Adding a generous amount of hot glue over the 230Vac connections are also a way to make working on this project a bit safer.

Fuses are also always mandatory for mains voltage related stuff, but your module apparently has a built in fusible resistor.

I do not see mounting holes. Adding these to the schematic is an easy way to never forget them on the PCB during updates. Designing the PCB is an iterative process and adding those holes to the schematic is the most robust way.

Labels are good, but overuse of labels makes schematics less readable. It’s always a bit of a tradeoff between readability and flexibility. In general, if stuff can be drawn with wires without labels, this is preferable, because if you see a wire without labels, you know instantly it’s just a wire. A label may appear on multiple places in the schematic. Usually it’s better to draw the “simple stuff” directly to the microcontroller, such as R5, R7 and D1.

Sometimes redundancy is good, but adding text to the schematic that a led is a led is not useful.

Why have you added the text “norm closed” to the relay? I’ve also interpreted the NC text as “Not connected” on the first read. Where are FLAG_ON and FLAG_OFF connected to? I only see them once. If you had drawn the three optocouplers with wires to the connector instead of with labels, there would also be a lot less labels to hunt around. The net highlight function does not work on your screenshot. (PDF output is searchable though).

Why use the HPLC3700? A quick peek suggests it costs over EUR5 each, while there are plenty of opto couplers starting form 50ct or so in small quantities. (I’m not sure about AC input versions.) I did not read the whole datasheet, but it seems that the HCPL3700 also needs a resistor on it’s input like any regular optocoupler. Maybe this is offboard somewhere? Datasheet of the HCPL3700 shows both some internal current limiting and a resistor (Rx) on the outside. What’s up with that?

50kOhm for R7 is quite high. Your BC337 may have not enough base current. When power consumption is important, use a mosfet here, (although the relay current swamps it).

With a regular optocoupler and an “anti-parallel” diode on the input these can also be used with AC. The led will blink @ your input voltage frequency, but your uC can usually easily compensate for that in software (Unless a quick response is needed).
What is the input voltage to your opto couplers? Is this also mains voltage? Mains voltage needs extra clearance on PCB’s, and PCB layout is important.

I always add a programming connector to PCB’s with a microcontroller.

I would remove the rectangular boxes around sub circuits. They only add distraction, and they are hard to maintain. Any change to the schematic is likely to force you to remove & redraw those boxes. Using the big bold text near schematic sections is a good practice though.

1 Like

Hey gmc, thank you for the material!

Got that, will add right now the decoupling cap.
J2 is there just in case it is needed a GND connection at 0V.

I will stay away from AC, as everyone suggested

Thank you for the examples.

I have the setup working and running on a breadboard with a 9V battery, that’s why I wanted to make the jump to AC, but as everyone said already, it’s to dangerous for someone with as little knowledge as me.

Hi Andy_P, thank you for your opinion, I will leave the AC idea behind until I have someone else more experienced working with me.

I was using a SmartProg2 to program the chips, didn’t even remember that since I am now using a different package I would also need to program it in a different way (ISP I believe).

About PB2, I tried to replicate what I had in my breadboard, 5V connected to button, when button is pressed 5V go trough a 10k resistor and then into ground and microcontroller pin.

That seems to be the way to go indeed, clearly AC is to much for me at the moment.

R10 is quite low.

LED voltage depends on LED color, but usually is between 1.5V and 2.2V.

(5-2)/110 = 27mA

This both exceeds what most microcontrollers can deliver comfortably, and probably also is excessively bright. 20mA was standard for LED’s until about 10 to 15 years ago. In these modern times leds usually have enough with just 1mA.

Hi paulvdh, thank you for the comprehensive answer.

About PB2, I tried to replicate what I had in my breadboard, 5V connected to button, when button is pressed 5V go trough a 10k resistor and then into ground and microcontroller pin.

The plastic box and the 5V routing seem a safer way to do this, I will definitely keep that in mind.

Will add the mounting holes, try to find the balance between wires and labels and reach a proper value for R7.

Originally I was using a normally closed SPST relay that would (or wouldn’t if activated) send a 230V signal to an exterior circuit. Honestly I can’t even remember why I changed it to what I showed here in the first place. But nevertheless, FLAG_ON and FLAG_OFF are signals to external circuits

The regular opto + anti parallel diode would decrease the cost of this project significantly, but I did not understand how the blinking of the LED relates to it. The opto’s input voltage is indeed the main’s.

I will have to add a way to program the chip through ISP.

As davidsrsb mentioned earlier he uses the dashed lines to show safety barriers, seems a much more useful application than mine :grin:.

Thank you once again for the attention in your response