.NET IoT is not something new; the encyclopedia is quite powerful, so there’s no need for me to elaborate too much here. The current era is different from when I learned QBasic; back then, I had to connect to the internet via a phone line with a miserable download speed of 3.5 kb/s. I also had to go to a store to buy an internet card. Once the internet card was used up (it was timed), I had to buy another one. Aside from the cost of the internet card, I also had to pay an additional fee of 0.2 yuan per minute (charged by China Telecom).
So, think about what concepts and terminology I didn’t understand back then; I had to look them up in books or run to the library. When I got to the library, I also had to pay a deposit of 100 yuan. The cost of going online was high, and the speed was slow, so I often didn’t bother to search online for many things.
Now it’s different; children today are so fortunate! They are online for over 20 hours a day. The internet is basically covered, so there’s no need to take terminology explanations so seriously; whenever needed, just look it up online.
I have a habit of rambling before I start, a bad habit developed over the years that I can’t change, so please don’t laugh.
I plan to write a series of articles mainly discussing hardware control development using .NET (it’s actually quite fun; I regret not having the opportunity to play with it back in college; otherwise, charming several junior girls wouldn’t have been a problem). Of course, to run .NET programs on a board, there are requirements for the development board—at least boards without an operating system won’t work. Among boards with operating systems, the Raspberry Pi is the most famous.
I’ve seen the Raspberry Pi in books before, but back in the days when online shopping wasn’t popular, I could hardly find one in physical stores (I didn’t even know what type of store to go to). So, I never had the chance to experience it. After that, I put it aside and didn’t think about it anymore. But after several years, I thought I still wanted to try it; otherwise, my coding career wouldn’t seem professional or perfect. So, vaguely, I remember buying one from a second-hand platform in 201x, not knowing which generation it was (definitely not the 3rd or 4th), and when I got it, I just installed the system and played with it for a bit. Then… it’s embarrassing to say, it gathered dust.
It’s been almost a year since the pandemic started; I believe everyone remembers that Spring Festival in 2020. Personally, I felt it was scarier than the SARS outbreak back in the day. During the SARS outbreak, we still attended classes normally; only a few factories were closed, and we didn’t wear masks; at most, we just used disinfectant powder to clean the floors. Perhaps it was due to the information lag back then.
So, I stayed at home and decided to tinker, thinking about modifying some appliances (especially smart lamps and DVD players) to see if I could create those fancy devices I saw in Ultraman as a child, allowing for remote control and being able to control things at home just by waving my hand—what we now call “smart home.” Watching others demonstrate always felt inadequate; it’s the sense of accomplishment from creating something myself that matters, even if I end up with something dumb, the key is that I experienced it personally.
The initial spark of thought was to use wireless networks to control various remote controls. I imagined placing a few infrared transmitters at home and running a web server on the Raspberry Pi, allowing various clients to control the infrared transmitters just by opening a browser, integrating the TV, set-top box, fan, lamp, and infrared camera at home. The remote control codes could be recorded using an infrared receiver module and stored in a database, retrieved from the database when needed.
With a sudden burst of enthusiasm, I quickly bought a Raspberry Pi 4 B. When I got it, I was amazed; wow, it was version 1.4, quite new!
Actually, that initial idea wasn’t fully realized; I only managed to control two DVD players and a “Mango Cloud” smart box.
Okay, I’ve rambled too much. This article serves as the beginning of this series; we won’t write code today; I’ll briefly discuss what tools and equipment we need to prepare.
1. Software Setup
In terms of software, it’s quite simple.
1. Development tools. Familiar Visual Studio or VS Code will do.
2. Development machine. It’s still better to write code on a computer; the configuration of the Raspberry Pi is relatively weak.
3. System on the Raspberry Pi. It’s better to use the official system. One thing to mention is that the official system has a lightweight version (Lite), which includes the core Linux functions without the desktop part or additional software, purely command control. This version is actually quite good, but there’s a persistent issue—when booting, it doesn’t automatically connect to the wireless network (even if configured, it doesn’t work) or keeps prompting that it’s searching for a network. However, installing the desktop version doesn’t have this problem. This issue has persisted, and the reason is still unknown.
2. Essential Components for Raspberry Pi Development Board
When buying a board, you need to purchase a Micro SD card, which is the memory card used in your phone. I recommend 32 GB; the prices for 16 GB and 32 GB are similar, so it’s better to go for 32 GB directly. If you have many memory cards, then you don’t need to buy one. Some stores on Taobao sell unbranded cards, claiming to be for Raspberry Pi, but they aren’t necessary at all. I’ve tested several Kingston cards (pulled from a dashcam) and SanDisk (used in mobile phones and TV boxes); after writing the system, they ran normally and at a decent speed. In short, any card will do.
Writing the system image is done on a computer (in most cases), so you need a card reader, or if you have a USB Hub with a memory card slot, that works too. I recommend buying a good one; poor quality ones can cause many issues. All the hubs and conversion cables in my home are from Ugreen; I’m not advertising; they really work well. I remember a couple of years ago, I bought an external optical drive, and the 0.8 yuan shipping data cable often failed to recognize the drive. When it did recognize it, it frequently disconnected while reading discs. I suspected the optical drive, but testing it with a drive from a Dell laptop also didn’t work. In the end, I bought a Ugreen cable. It recognized it immediately and didn’t disconnect. It’s true, you get what you pay for.
Regarding power supply, the power port of the Raspberry Pi seems to vary with each generation; the one I bought is the 4th generation, which uses a Type-C port. The original power supply is quite expensive; you can buy alternatives as long as they are 5V / 3A. Additionally, there are two options:
1. Power bank. New models can output a maximum of 3A.
2. USB Hub with external power supply. Something like this.
It’s claimed to be industrial-grade, can supply power normally, with an external power supply of 12V/4A, and the output is 5V. USB hubs without external power cannot power it.
3. Dry batteries + battery box. This method is not very reliable; as the battery power decreases, the voltage drops, which can easily cause it to hang.
4. Adjustable power supply. This allows you to adjust the voltage; unless you’re willing to buy an expensive one, the cheap ones aren’t very reliable. I’ve bought cheap ones before and have been shocked by them twice.
In practical testing, 5V/2A and 5V/1A power supplies can keep the Raspberry Pi running normally. In general, the current size is determined by the load; the key is the voltage, which should be selected as 5V. If the voltage is too high, it may upgrade the Raspberry Pi to the “smoke” version.
3. Other Equipment
1. Breadboard. It’s convenient when there are many connections.
2. Dupont wires. You definitely need these; at least buy them in the length you need; for experiments, 10-20 CM is sufficient.
3. Case. This is optional, but it protects the Raspberry Pi and helps with heat dissipation (plastic ones probably won’t dissipate heat well, so heat sinks should be attached). The case I bought is metal, and it’s quite good; the CPU stays around 36 degrees.
4. Ethernet cable. I directly use the cable that came with the router; it’s simple and quick. Although the Raspberry Pi has a wireless network card, a wired network card is faster. Connecting the Ethernet cable directly to the computer’s Ethernet port allows for faster SSH login to the system.
5. “T” shaped expansion board. Since I put the Raspberry Pi in a case, connecting wires became inconvenient, so I bought a T-shaped board to expose the 40 pins, making wiring much easier.
The image above is borrowed, source unknown. When connecting, ensure the direction is consistent; do not reverse it.
6. Electronic magnifier. The electronic modules are small, and if your eyesight isn’t good, it can be challenging to operate. Some areas are difficult to handle even with good eyesight. You can buy an electronic magnifier with LED lights for illumination. Connect it to the computer via USB; it works like an external camera, and you can use the built-in camera app in Win10. You don’t need to buy a powerful one; just one that magnifies and has a light will do, usually around 50 yuan.
7. Multimeter. This isn’t necessary but can be useful for measuring voltage and resistance.
8. Fire extinguisher. Some tutorials mention this, but I think it’s not that serious; generally, operations won’t cause fires.
9. Anti-static brush. I think this is optional; there’s a simple way to release static electricity; just ground yourself by pressing your hands on a wall or the floor. However, I have an existing anti-static tool at home, originally used for vinyl records. If you’ve used a turntable, you’ll know that excessive static can cause crackling noises when playing records. My turntable has issues; the circuit that installs the needle isn’t very good, and excessive static can cause the amplifier to fail to output, resulting in intermittent sound. If you’re worried that static electricity might damage the Raspberry Pi and electronic modules, you can get a brush from Taobao and brush it a few times before touching the device.
4. Other Development Boards
The Raspberry Pi is considered a relatively advanced board; it has an operating system, and often you can use the Raspberry Pi to control other boards, especially when using multiple modules; you can’t connect everything to the Raspberry Pi. At this point, you can consider delegating some work to other development boards, with the Raspberry Pi as the main control center directing the other boards.
For example, the board below is also quite good, imported from Italy. It costs over 100 yuan, which is cheaper than the Raspberry Pi. In their country, these aren’t considered expensive, but they become a bit pricey when converted to RMB.
If you think the above board is expensive, the two below are cheaper, costing over ten yuan each.
Of course, as the saying goes, you get what you pay for. The above two are domestically produced, and I don’t know which manufacturer they are from, but there are plenty on Taobao. Be aware that these non-original boards require manual driver installation when connecting to the computer; you can find drivers for the CH34x chip online, generally CH340 and CH341. If one version doesn’t work, you can try several; installing drivers for non-official versions can be a headache. Original motherboards can be automatically recognized by Win10.
These cheaper versions are not very stable and often encounter upload program failures; some soldered pins may be loose or bent, making them not very user-friendly, but they are usable. I bought two Nano boards; one I made into a thermometer for long-term use at home. The other one overheats severely and is hot to the touch, but the reason is unknown.
Arduino boards, unlike the Raspberry Pi, cannot run an operating system; the Raspberry Pi is a complete computer, but relative to a regular computer, its performance does lag (no matter how the official propaganda goes, that’s the reality). Therefore, you can use .NET (C#/VB.NET), Go, Python, or directly C++ on the Raspberry Pi; while Arduino uses C++ language.
Regarding communication between development boards, you have the following options:
1. Wired connection using serial communication (cross-connect TX and RX pins). This solution is good and easy to handle, similar to reading and writing text files.
2. Wired connection using standard protocols like IIC/I2C, SPI, etc. Let the Raspberry Pi be the master, and the other boards be the slaves. The Raspberry Pi can command the other boards to work. It’s like the Raspberry Pi is the boss, and the other boards are workers. Raspberry Pi: “Hey, help me measure the water pressure,” Board A: “Yes, sir.”
3. Wireless serial port, such as the ESP8266 series; the method is similar to general serial ports, but it can transmit wirelessly, achieving transparent transmission.
4. Self-designed protocol. Devices can combine various communication rules through high and low levels and the duration of signals. Therefore, you can design this protocol entirely yourself. For example, when Board B sends a byte to the Raspberry Pi, you can agree that each binary bit starts with a low level and lasts for 50 microseconds; after 50 microseconds, if it remains low, it indicates 0; if it goes high, it indicates 1; then pull the level low and high again for 100 microseconds to indicate the completion of one bit. Repeat this 8 times to complete sending one byte.
5. Writing System Images
There are plenty of tutorials on this.
However, I’ll add some content.
1. The SD card doesn’t need to be formatted; testing has shown no issues. Before writing the system image, you can use Windows’ built-in Disk Management tool to delete the partition (or volume) of the SD card, then open Win32DiskImager and directly select the SD drive. This software can automatically recognize removable devices, and your hard drive partitions won’t appear in the selection list.
2. Set a fixed IP; this mainly refers to when connecting with an Ethernet cable to the computer. If you don’t set a fixed IP, the IP address of the Raspberry Pi will be different each time it starts, which makes it quite annoying to connect. There are many tutorials online; just follow the steps to modify the .conf file. However, I’m going to introduce a very simple method below.
Once you successfully write the system image, you will see two partitions, as shown in the image below.
For instance, I have one labeled as boot on drive F and another on drive G. This G drive is a file system that Windows cannot recognize; every time you connect, Windows will prompt you to format it. You must absolutely not format it; just ignore the prompt.
Open the boot partition; there are many files, including two .txt files that can be opened directly. It’s best to use tools like NotePad++ to edit them, mainly because Windows uses carriage return characters in its line endings, which are not used in Linux.
The config.txt file is used to configure which settings/functions to enable on the Raspberry Pi, such as enabling I2C, enabling 1-Wire (one-wire bus), etc. You can leave this file unchanged; after logging into the system, you can modify it using system commands—raspi-config. Some tutorials will tell you to disable Bluetooth because the main control’s serial port module is assigned to Bluetooth, and the TX and RX pins use chips from the microcontroller (secondary), so they need to be swapped. However, this doesn’t significantly impact unless you have stricter efficiency requirements. Of course, you don’t have to disable Bluetooth; you can add this line in the config.txt file to swap the two serial controllers without disabling Bluetooth.
dtoverlay=miniuart-bt, krnbt=on
Detailed explanations of these configurations can be found in the /boot/overlays/README file, which you can glance at when you have time.
Now, the key point: the other text file is cmdline.txt, which stores some commands that will be executed when the system starts; each command is separated by a space. Open the cmdline.txt file and add ip=192.168.11.25 at the very beginning of the file; the value after the equal sign is the static IP address you want to use, which you can decide.
ip=192.168.11.25 console=serial0,115200 console=tty1 root=PARTUUID=2e9f1d8c-02 ……
Isn’t this simpler than modifying the .conf file? Plus, it sets a global IP address.
Finally, remember to create an empty file in the root directory of the boot partition; the file type doesn’t matter, just as long as it’s empty, name it SSH without an extension, so that when the system initializes, the SSH function will be enabled, allowing you to log into the Raspberry Pi without a display, keyboard, or mouse using just an Ethernet cable.
Windows 10 comes with an ssh tool, so you don’t need to look for tools everywhere. Open the command prompt and enter:
ssh [email protected]
Here, pi is the username; this is the default username for the system, and the default password is raspberry. After logging into the system, it doesn’t matter whether you change the password; it’s just for your own use. If you take it outside or if someone else might access your Raspberry Pi, you should change the password, and it’s better to change the username and hostname as well.
The first time you connect to a Linux host on Win10, it will ask you a string of XX that you can’t understand; don’t worry about that string; just type yes and hit enter. If an error occurs, go to C:\Users\
6. Purchasing Modules/Sensors
If you buy a Raspberry Pi just to use it as a web server or to learn the Linux system, then you don’t need to spend money on various electronic modules. However, I think it’s a bit excessive to buy a development board just to learn Linux; I suggest you sell it on Xianyu for a third of the price. Since it’s a development board, you should play with the hardware; otherwise, you’re not getting your money’s worth (I bought a 2GB memory version for 268 yuan; 4GB/8GB versions are more expensive).
As for which modules and sensors to buy, that’s up to your preference.
1. Control button types. You can buy momentary switches, touch switches, thermal switches, light-sensitive switches, sound-controlled switches, etc.;
2. Sensor types. For example, MPU 6050 (three-axis accelerometer and gyroscope, this is a bit complex), PAJ7620 (gesture sensing), DHT11 (temperature/humidity detection, this is simpler), DS18B20 (temperature detection, 1-wire protocol, which can be accessed in Raspberry Pi in a file manner, and is simple to use). Many sensors use the IIC protocol (i2c), but you don’t need to simulate it yourself; .NET has related Nuget packages.
3. Motors. Servos are quite fun; they can control the rotation angle, and you can use them to make robotic arms.
4. Light-emitting diodes, LED lights. Remember to buy 200 – 250 Ω resistors; this range of resistance is sufficient. I’ve done experiments showing that LEDs are fully lit when the current reaches 18mA. The maximum output current from the Raspberry Pi is 50mA (that’s what the manual says), but the output current per pin shouldn’t be that high. So, if you’re daring, you can connect the LED light without a resistor and run it bare. Actually, diodes won’t burn out; I’ve lit them with 80mA of current overnight without burning out. To verify what’s said online, I specifically burned two lights, one white and one red.
The white light burned out when the current reached 280mA, while the red light exploded around 250mA. When the current reached 190mA, the light flickered, heated up quickly, and made a pleasant sound. So, to sum it up, using a 200-ohm resistor or running it bare is completely fine.
The GPIO pin high level (voltage) is generally 3.3V, and the diode works between 2 to 2.2V, with a current of 18 – 23mA (estimated, as different colors have different currents). Let’s assume the current is 20mA; let’s calculate:
( 3.3 - 2.0 ) / 0.02
The result is 65 Ω, so a 200 Ω resistor is safe enough.
5. Ultrasonic distance measurement module (like the HC-SR04 module). If you want to make a toy car, this is useful; it’s better than laser distance measuring. Based on my experiments, ultrasonic can measure up to 5 meters; it should be more accurate in the 2-4 meter range. This is just experimental data and is not representative; you can try to see how far it can measure.
The above are just a few examples; there are many modules, and I can’t list them all.
When buying these modules, I suggest not to buy them all at once; you can buy them separately or in small batches. Although it’s a hassle to receive multiple packages, it’s relatively cost-effective, as you can buy from whichever store is cheaper. However, the common express delivery services are just a few, and if multiple packages arrive at the same time, you can collect them together. Sometimes, the same module is sold at a premium by some stores; if you buy a lot at once, you’ll clearly end up overpaying for some modules.
7. Raspberry Pi Pins
Although there are 40 pins, you can only use 28 of them. Regarding how to count the pin sequence, be careful not to count it backward; counting backward can easily lead to wrong connections, which could burn out the circuit. Face the pins toward you, with the USB port of the development board facing down, and count from left to right, top to bottom. See the image below.
Or see this image.
You can download the Raspberry Pi datasheet from here: Click hard
Count them; there are two 3.3V, two 5V, and eight GND (power negative), which takes up 12 pins, leaving 28 pins available for data IO, including special-purpose pins for serial communication, IIC, SPI, etc. Pins labeled GPIO XX can generally be used freely. Of course, you can slightly expand the number of available pins by modifying the pin reuse configuration.
Pin reuse (or multiplexing) is represented by a value called ALT, which is documented in the datasheet. Alternatively, you can check this: https://pinout.xyz/, which introduces the functions of each pin.
For example, GPIO 12 is the 32nd pin on the board (the fifth pin from the bottom up on the right row, above GND).
Leave a Comment
Your email address will not be published. Required fields are marked *