# SimpleBedsideLamp
Renesas? Swiss roll? Pizza? I can't tell them apart. I've only heard of the name but never used Renesas' Arm chip. I can't miss this opportunity.
Then... light a lamp. I asked the camp administrator. It's great that the newbie can end the camp by lighting a lamp!
(For ease of understanding, some hardware parameters and software environment are compared with popular ST products from friendly companies.)
## Planning analysis
is basically a simple deletion of the training camp routine. **I hope Sister Orange can rate it as a novice area**
Let's take a look at the training camp case function
1. R7FA2E1A72DFL is used as the main control chip of the electronic clock
2. A four-digit 0.56-inch common cathode digital tube is used to display the time
3. The ambient temperature is collected through the DHT11 temperature and humidity sensor
4. Four buttons are used for clock setting buttons
5. A reset button, serial port circuit, and SWD download circuit are brought out for easy download and debugging
6. The Renesas chip integrates a touch pin, and a touch pin is brought out to be used as a touch button
7. Use a passive buzzer as a reminder of time reaching, etc.
8. Use AO3401A+1N5819 diode to form a power switching circuit
9. Use TP4056 to charge the battery
##### Core: heart:
The training camp requires the use of the RA2E1 series `R7FA2E1A72DFL` chip, which cannot be changed.
##### Electricity: electric_plug:
What is the most important thing to make the main control run? Jiaran!!! Ahem
. It's the power supply. The lights should consume a lot of electricity, so delete the battery and charging parts in the training camp first! (●'◡'●) Electricity is the most terrible.
Just plug in the charger, hehe, the infinite approaching electric energy rolls in
##### Light: sunny:
Lighting naturally requires a light. The digital tube is essentially a light, so it can only be regarded as a *substitute*.
*Make a bright white light, bright*
*Make a colorful little light, add performance*
Four-digit digital tube has 32 lights in total, and the number is reduced a little bit here to 11 lights
##### Test: eyes:
The clock is matched with a temperature and humidity sensor*, but it doesn't look so matched. **Replace** it with an acceleration sensor*, and only change two words, a small change.
You have to look at the clock during the day, and you can't see it clearly with low brightness. You also have to look at the clock at night, and it's so bright that it's dazzling. Isn't it better to make a function that automatically adapts to the ambient brightness, and add a brightness sensor. But... There is no official example, and I'm a farmer, and I don't know how to do it, so I'll leave a place and wait for the official. See clearly. **It's just a reserved position without adding functions**
##### Hand: muscle:
There must be a hand to turn on the light (in case it doesn't light up). The example has four physical buttons and one touch button. Delete four buttons (*^_^*)
##### Chaos&&
There is still a buzzer left, it is very noisy, delete it!
What about the reset button/serial port circuit/SWD download circuit, all weird, delete them!
Finally, Orange said it would be better to make a shell, and Xiaobai, who is a modeler, doesn't know how to do it. So I had to make a small moon.
In summary, the small lamp is essentially an example with a few minor changes. This must be a **newbie camp**!
## Circuit Design
### MCU
MCU is the core of this circuit, so read the MCU manual first. Look at DS, what's the difference between this :heart::
- It is recommended to check the English manual! The official Chinese machine-translated version is completely unproofread, pure machine translation, obsessive-compulsive "ecstasy"
![image.png]

- The first line starts with `Ultra Low Power`, wow, ultra-low power chip, can we make a little one powered by a button battery? Jump directly to the power parameters section to check. The more I look at it, the more I feel something is wrong. I took out the old version of ST's M0+ chip to compare it, and the difference is a bit big. I went to the official website of Renesas to learn more and learned that its low-power series is RA2L1 (also starts with UltraLowPower, guess it is the same template but forgot to change it), and RA2E1 is an entry-level product, and its counterpart should be ST's F0 series. Check the price of RA2L1, Lichuang 20+, Taobao 6.50. Well, I'd better use RA2E1 obediently (although the price also increased during the training camp)
- M23 core is good, better than the antique M0/M3 that many domestic manufacturers still use for free license. However, M23 is not new, it was released at the end of 2016. M23 is still positioned as a low-end entry-level MCU, and the same level is M0
pay attention to the complete body and core parameters, and start to pay attention to the details of peripherals.
#### RTC&CGC
clock accuracy, the built-in SOSC (LSI) in the DS shows that its full voltage domain clock range (temperature environment is not mentioned in the DS) is 27.8528~37.6832KHz, and the error is about ±15% calculated based on the center frequency 32.768KHz. Compared with the friendly G030, its full voltage domain (2.0~3.6V) full temperature domain (-40~85℃) LSI range is 29.5~34KHz, and the error is about 7% calculated based on the range center 31.75 (the maximum error is about 10% based on 32.768KHz). It can be seen that the built-in RTC performance of RA2E1 is not good. Although the training camp routine uses the built-in RTC, an external crystal oscillator is still used here to improve the accuracy (CGC: xcin&xcout).
![image.png]

Power-off timekeeping. After all, no one wants to reset the time every time the power is plugged in or unplugged. That would be very troublesome. The RA2E1 chip does not have an external Vbat pin. Generally, small packages or chips that are confident in the low power consumption of the chip will not lead out Vbat separately. Instead, the software is required to enter low power mode immediately after the external power is switched to the battery after power failure. This is a mandatory requirement for showing off muscles. This has some requirements for circuit design. The battery will power the entire MCU. Since a fixed power supply is used, the external power supply is always 3.3V, which must be fixed higher than the button battery voltage, and the circuit design is simpler. The only thing to note is that the diode needs to be selected with a small forward voltage drop and a small leakage current. Give the chip an external power insertion signal to wake up the chip (built-in PVD detection is also possible). Here, the signal is given to the Wakeup pin, which is the NMI interrupt pin. The corresponding application manual was not found on the official website, so the specific circuit refers to the AN4718 of a friendly company.
Configure RTCOUT to output a 1pps signal to measure the accuracy of RTC timekeeping.
For the convenience of testing, new chips generally lead out MCO, which is reused here as the enable pin of the white light LED.
#### CTSU
CTSU is actually a touch peripheral. Although this chip supports mutual capacitance solutions, I checked several official development boards and none of them use it. I wanted to find a reference and asked FAE twice in the training camp group, but was ignored. The official has not designed a mutual capacitance board, so I won't be rash and just stick to the self-capacitance solution. The
essence of touch recognition is to check the capacitance frequency. The chip charges the touch electrode (capacitance). When the pin voltage is lower than the low threshold, it starts charging and reaches the high threshold. One charge and one discharge is a cycle, and one count is captured in each cycle. When the system is static, the capacitance is constant, so the charge and discharge cycle is constant, that is, the count value is constant. When the finger/other object is close to the electrode, the system capacitance is increased, which leads to an increase in the charge and discharge cycle.
Then this system capacitance includes Which ones? There are generally several capacitors on the PCB, including Ctrace (line with reference ground), Cgroud (MCU reference ground and earth), Celectrode (electrode with reference ground), Ccomponet (parasitic capacitance in the chip), and Ctouch (capacitance formed by the finger and the electrode at the reference ground and the earth). It can be seen that `ΔC==Ctouch`, Cgroud is a fixed value, and the rest can be collectively referred to as Cp. As Cp increases, the sensitivity decreases, and as Ctouch increases, the sensitivity increases. At the same time, due to the chip capabilities, the total value of C should be less than the chip upper limit (50pF).
Let's talk about the simple touch electrode optimization direction first: (The open source platform Markdown does not support formulas, so the formulas are not posted)
- The electrode needs to be tightly connected to the cover layer. If the gap is greater than 2mm, a spring or other method should be used.
- The larger the dielectric constant of the cover layer, the better. Usually, materials with a dielectric constant between 2 and 8 are used. Conductive materials cannot be used. **Be careful with paints/coatings containing metal particles**
- The smaller the thickness of the cover layer, the better. The larger the area of the touch electrode, the better.
Then comes the highlight, the way to reduce parasitic capacitance in PCB design:
- The order of the impact of Cp is generally: line length> copper coating near the electrode> distance between the line and the copper coating>> number of vias> electrode area ≈ line width.
- The impact of chip packaging, different chip packaging leads to differences in gold wire bonding, current changes in the chip's internal peripherals, and high-frequency actions will affect the touch peripheral part. This part requires testing of the chip, and some professional manufacturers will mention it in the manual (such as IO32 of ESP32). It is recommended to select touch pins that are less affected by this and other designs to reduce the instantaneous current fluctuations on the chip.
- The PCB substrate is commonly made of FR4 material. When the board thickness is reduced, the copper coating on the back of the electrode should be reduced or eliminated. The discussion here is based on this. After all, we have never used FPC.
- The line length should not exceed 300mm (there will not be such a case in reality), and the count value will decrease by about 15% for every 100mm increase in length
- The gap between the line and the copper cladding (reference ground) should be greater than 1mm.
- The line should be surrounded by copper cladding to increase anti-interference ability. The line and the copper cladding should be separated by more than 1mm (this value is affected by the thickness of the covering layer). The recommended copper cladding ratio is 20% (6mil line width, 50mil spacing). The copper cladding grid should intersect the line at 45 degrees. The interval with solid copper cladding is more than 5mm. When solid copper cladding is used on the bottom of the electrode, the count value will decrease by about 15%, and when grid copper cladding is used, it will decrease by about 10%. At the same time, the copper cladding should be reasonable, otherwise it is recommended to divide the copper cladding area to prevent the actual copper cladding from becoming an antenna for a high-frequency signal.
> Many people are opposed to the copper coating. Engineer Mo has reminded not to coat the back with copper. Because the copper coating pattern will couple with the electrode/line capacitance, thereby increasing Cp and affecting sensitivity. In practical applications, it is recommended to increase the grid copper coating, which can suppress some noise and increase the anti-fluctuation ability (the electrode can be regarded as an antenna at this time). When there is electromagnetic noise or radio frequency environment in the application environment, the bottom copper coating should be increased. However, the design should be strictly in accordance with the manual recommendations. For example, the design guide of RA requires a 5mm spacing between the electrode and the grid copper coating, and the copper coating width should be less than 5mm.
>
> RA has chips that support active shielding, which is recommended. However, the training camp's designated chip RA2E1 does not support it.
- The fewer the number of line vias, the better. The vias should be at the edge of the electrode to reduce the parasitic capacitance between lines on different layers. When 10 vias are added, the count value drops by about 8%
- Try not to run wires under the line/electrode. The wires should intersect the touch line at 90 degrees. Parallel wires should be spaced at least 4mm apart to reduce the area facing the same line. High-power signal lines (carriers) such as electrode drive power lines and radio frequency lines should be kept as far away as possible and shielded. (In fact, the touch circuit can be wired according to the weak analog signal wiring rules)
- The spacing between multiple lines is more than 1.27mm.
- The electrode size should fit the finger size. When the thickness of the cover increases, the electrode size should be increased appropriately, usually around 12mm. The electrode shape is best in a round shape. It is not recommended to use a shape with sharp corners (charges will accumulate at sharp corners). The electrode spacing should be greater than 5mm (it should also be increased when the thickness of the cover increases)
> The width of each slider should be greater than 8mm, the slider spacing is 0.5mm~2mm, and the height is about 12mm. Common shapes are V and W types
>
> Duplex slider (one-dimensional matrix), two blocks are arranged for the same pin on the slider, and the software compares the values of adjacent blocks to determine the actual touch position.
>
> Matrix keyboard, same as mechanical keyboard, uses N+M IOs to identify N*M keys. The electrode pattern uses four-leaf clover