The pins of the WeAct Duino are shown in WeAct_Duino_Pin_Map_R1.png, available in the Files section. The physical pin numbers are highlighted in green. Usually, we won't refer to them by physical number. Let's discuss their capabilities.
Digital Pins: All pins indicated in the WeAct_Duino_Pin_Map_R1.png as digital pins (highlighted in gray) work as expected except D1 and D2. These pins (C14 and C15) are connected as clock oscillator pins for the 32.768KHz crystal for the Real Time Clock module. It is possible to remove the crystal and the two associated capacitors, then install jumpers SB1 and SB2, then D1 and D2 will function as expected. Note D0,D1, and D2 all have very limited output drive and should not be used to drive leds or other moderate current loads. Since it seems that these pins are intended as inputs (pushbutton on D0) or clock drive, I can't really get excited about testing them as digital I/O. I'm more interested in using the Real Time Clock capabilities. (That will be the subject of a Project Log soon.) So just ignore D1 and D2; D0 is the user button. All digital pins can source or sink 20mA except D0,D1, and D2 (C13, C14. C15). Those pins are limited to 3mA. The total current from all I/O pins is 100mA max.
Analog Read: All pins A0-A15 (mustard highlight) have analog read capability. The default resolution is 10 bits (0 to 1023) and the input level can be 0 to 3.3V. Using analogReadResolution() allows the resolution to be set to 6,8,10,12,14, or 16 bits (all tested). Note that the setting will apply to all analog input channels. They can't be set individually.
Analog Write: All digital pins with the letters "pwm" beside them are capable of pwm output. The default frequency is 1KHz and the resolution is 10 bits (0 to 1023). Using analogWriteResolution(),the resolution can be set to 6,8,10,12,14, or 16 bits. With analogWriteFrequency(), the pwm frequency can be set from 1Hz to as high as 400KHz. Using analogWrite() with plns A4 and A5 (digital pins 7 and 8) will use the DAC so a true analog voltage level will be output. The default resolution is 8 bits and analogWriteResolution() may be used to increase the DAC resoution - 12 bits is the maximum regardless of the setting.
5V tolerance: The pins indicated with a red dot next to the pin number are 5V tolerant. This means that an input signal from a device powered by 5 volts can be use without damaging the WeAct Duino. Configure the pin as an INPUT only, do not use a pull-up or pull-down. Providing a suitable 5V output is less of a problem. A 3.3 volt output is a sufficient 5V high input, so no pull-up is usually required. If one is needed, then use OUTPUT_OPEN_DRAIN and pull up to 5V to control the 5V peripheral. Any digital pin can be used like this.
External Interrupts (digital pin interrupts): External interrupts are described here. An Uno has two external interrupts; but the WeAct Duino has one available on every digital pin. There are two restrictions. The first is that only 16 total external interrupts are available. (That's still a lot!) Although any digital pin is a potential external interrupt, the rule is that pins having the same physical pin number in different ports conflict (like A5 and B5). They get mapped to the same EXTI line. Only the last one used by attachInterrupt() will actually cause an interrupt. There is no compiler warning of this behavior. Refer to WeAct_Duino_Pin_Map_R1.png for the physical pin numbers. The only modes supported by attachInterrupt() for the WeAct Duino are RISING, FALLING, or CHANGE, not LOW or HIGH. It is unnecessary to use the digitalPinToInterrupt() function, just the digital pin number is enough for attachInterrupt(), but you must be aware of the physical pin number to avoid possible conflicts.
Serial I/O: Using the USART (serial) capability of the WeAct Duino is actually pretty easy. All that's required is to instantiate a HardwareSerial object (call it mySerial, for example), and use it as you would use Serial in an Uno sketch. When you instantiate the HardwareSerial object, find the pin numbers to use from WeAct_Duino_Pin_Map_R1.png. Specify the digital pin numbers, Rx first, then Tx. The other necessary step is to select "Enabled (no generic 'Serial')" from the Tools / U(S)ART support menu. It is not necessary to #include HardwareSerial.h to support the serial capability. Note that the pin numbers you pass in will determine which of the five available USARTs will be assigned to the name you use. For example, pins 23 and 24 will cause USART 1 to be used (the Rx pin is first, then the Tx). Make sure you pick Rx and Tx pins that are on the same USART. Now you can connect to Tx and Rx with a serial to USB interface, use the one provided with the WeAct ST-Link, or connect to another serial device, such as a GPS receiver. There are 5 serial ports available: 1,2,3,4, and L (Low Power). While the Low Power Serial port has some special properties, you can just use it as a standard serial port. If the device you connect the serial port to runs at 5V, be sure to select serial port inputs (RX) that are 5V tolerant.
Example using External Interrupts and Serial output: Time for an example or two to really see how serial and external interrupts work!
For this example, we'll use the HC-SR04 ultrasonic distance sensor. If you're not familiar with it, have a look here. You'll find a good explanation of the HC-SR04 and how it works, and an example program for an Uno. Stop reading when you get to the "Arduino Ultrasonic Sensor and LCD Display Example" part. We aren't using an Uno, nor do the libraries suggested work for the WeAct Duino. 5V is needed to power the HC-SR04 and the response from it is a 5V pulse.

It needs a pulse to start, then returns a pulse which is timed to determine distance. We need to supply 5V and handle the 5V pulse. So we simply use one of the 5V tolerant pins. No problem! 5V power is supplied to the Vcc pin.
Connect the ST-Link to the WeAct Duino as described in the Blinky Project Log, except don't hook up the 3.3V from the ST_Link. Hook the purple wire (RXD) from ST-Link to 24 (A9 - TX1) on WeAct. From the HC-SR04, hook GND to GND on the WeAct, hook Trig to 17 (B13) and Echo to 18 (B12). Finally, hook up a 5V power supply with GND to GND and 5V to VCC on the sensor and the WeAct (NOT 3.3V). Here is the connection diagram. The White wire is shown as a dashed line.

The files we will use go in the Sketchbook directory. Find your Arduino Sketchbook location from the Arduino IDE using File > Preferences. The Sketchbook location is the first entry in the window. We will consider two ways to time the return pulse from the HC-SR04. An example program for each method is provided. To begin, download G431[etc].zip from the Files area and unzip it into your [Arduino] directory. Then open it in the Arduino IDE using the File > Open command. This program is just like the example program in howtomechatronics, and will use the pulseIn() function to time the HC-SR04 response. Let's look at the Serial capability first. We declare a HardwareSerial object and specify what pins to use. The digital pin numbers can be specified and we get those from WeAct_Duino_Pin_Map_R1.png. Specify the Rx pin first, then the Tx pin. In this example, pins 23 and 24 will cause USART 1 to be used (the Rx pin is first, then the Tx). If you followed the instructions for wiring this example, you will have connected the Tx to pin RXD of the WeAct ST-Link. As discussed above, to use the USART capability, select Tools > U(S)ART support: "Enabled (no generic 'Serial')". Then you can use your new serial port just like Serial with an Uno. Now compile the program and upload it, then start the serial monitor. You should see a steady stream of readings from the HC-SR04.
Let's look at another way to drive the sensor. The only disadvantage of pulseIn() is that the function blocks, that is, no other code can run until a pulse is returned or a time-out occurs. Sometimes, that behavior can cause problems. Download and unzip G431HC_SR04_interrupts.zip into the Sketchbook area. Open the file in Arduino. Here you see another way to time the return pulse using an External Interrupt on the pulse return sensing (echo) pin. We configure the interrupt to occur whenever the state on the echo pin changes. when that happens, the interrupt service routine, echo_interrupt(), runs. So when the trigger happens and the echo line goes high, we record the echo start time. Then when the echo returns and the line goes low, we record the echo end time. The difference is used to calculate the distance, just like the previous example. In setup(), we attach the echo interrupt routine to the echo pin to occur whenever the state of the echo changes. In loop(), we set the echo_end to zero and pulse the trigger pin. Then we monitor the echo_end pin in a while() loop. When it is non-zero, we know that the echo has returned and can calculate the echo_duration and thence the distance. Yes, the while() loop used in this manner is blocking, but a change in logic could make it non-blocking. That is not possible using pulseIn().
Look carefully at the operation of echo_interrupt(), the interrupt service routine. Because of the way attachInterrupt() is called in setup(), echo_interrupt() will be called whenever the state of echoPin changes. To start the process, echo_end is set to zero and a pulse is applied to trigPin. The HC-SR04 will make the echoPin go high because of the trigger, and will emit an ultrasonic pulse. The trigger will cause echoPin to change to a high and an interrupt will occur that will cause echo_interrupt to run. Since echoPin is high, echo_start will be set to the current micros() reading. The echo interrupt will return. Since echo_end is still zero, the while() loop will continue to run. When the ultrasonic echo returns to the HC-SR04, the echoPin will change to a low. The echo_interrupt() will again occur. This time, echoPin is low and echo_end will get the current value of micros(). When echo_interrupt() returns, echo_end is no longer zero, so the while loop ends, the distance is calculated, and the result is printed to the serial port. After a delay, the process repeats.
This example has shown how to use the USARTs to provide serial I/O, how to use a 5V tolerant input, and how to configure and use an external interrupt. Not bad for one example!
doctek
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.